<?php

namespace App\Api\Controllers\User;

use App\Api\Controllers\BaseController;
use App\Api\V2\Controller\Order\User\OrderQueryController;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Store;
use App\Models\StoreDayAllOrder;
use App\Models\User;
use App\Models\UserDayOrder;
use App\Models\UserMonthOrder;
use App\Models\WeixinSharingOrder;
use App\Services\Order\UserService;
use App\Services\PrintSqlService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

class OrderController extends BaseController{

    // public function order(Request $request){
    //     try {
    //         $user = $this->parseToken();
    //         $store_id = $request->get('store_id', '');
    //         $user_id = $request->get('user_id', '');
    //         $pay_status = $request->get('pay_status', '');
    //         $discount = $request->get('discount', '');
    //         $ways_source = $request->get('ways_source', '');
    //         $company = $request->get('company', '');
    //         $device_id = $request->get('device_id', ''); //设备id
    //         // print_r($store_id);die;

    //         $ways_type = $request->get('ways_type', '');
    //         $time_start_s = date('Y-m-d 00:00:00', time());
    //         $time_start_e = date('Y-m-d 23:59:59', time());

    //         $time_start = $request->get('time_start', '');
    //         $time_end = $request->get('time_end', '');

    //         if (in_array($time_start, [null, ''])) {
    //             $time_start = $time_start_s;
    //         }

    //         if (in_array($time_end, [null, ''])) {
    //             $time_end = $time_start_e;
    //         }

    //         $amount_start = $request->get('amount_start', '');
    //         $amount_end = $request->get('amount_end', '');

    //         $re_data = [
    //             'time_start' => $time_start,
    //             'time_end' => $time_end,
    //         ];
    //         $check_data = [
    //             'time_start' => '订单开始时间',
    //             'time_end' => '订单结束时间',
    //         ];
    //         $check = $this->check_required($re_data, $check_data);
    //         if ($check) {
    //             return json_encode([
    //                 'status' => 2,
    //                 'message' => $check
    //             ]);
    //         }

    //         $sort = $request->get('sort', '');
    //         $out_trade_no = $request->get('out_trade_no', '');
    //         $trade_no = $request->get('trade_no', '');

    //         $day = date('Ymd', strtotime($time_start));
    //         $table = 'orders_' . $day;

    //         if ($out_trade_no || $trade_no) {
    //             $a = array('a', '_', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
    //             $b = str_ireplace($a, "", $out_trade_no);
    //             $day = substr($b, 0, 8);
    //             $table = 'orders_' . $day;
    //         }

    //         //限制时间
    //         $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
    //         $day = 31;
    //         $now_time = date('Y-m-d H:i:s', time());
    //         $end = date('Y-m-d 22:00:00', time());
    //         // if ($now_time > $end) {
    //         //     $day = 31;
    //         // }

    //         if ($date > $day) {
    //             return json_encode([
    //                 'status' => 2,
    //                 'message' => '时间跨度不能超过'.$day.'天'
    //             ]);
    //         }

    //         //跨天操作
    //         $time_start_db = date('Ymd', strtotime($time_start));
    //         $time_end_db = date('Ymd', strtotime($time_end));;
    //         $is_ct_time = 0;
    //         if ($time_start_db != $time_end_db) {
    //             $is_ct_time = 1;
    //         }

    //         if (env('DB_D1_HOST')) {
    //             //有没有跨天
    //             if ($is_ct_time) {
    //                 if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                     $obj = DB::connection("mysql_d1")->table('order_items');

    //                 } else {
    //                     $obj = DB::connection("mysql_d1")->table('orders');
    //                 }
    //             } else {

    //                 if (Schema::hasTable($table)) {
    //                     $obj = DB::connection("mysql_d1")->table($table);

    //                 } else {
    //                     if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                         $obj = DB::connection("mysql_d1")->table('order_items');

    //                     } else {
    //                         $obj = DB::connection("mysql_d1")->table('orders');
    //                     }
    //                 }
    //             }
    //         } else {
    //             if ($is_ct_time) {
    //                 if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                     $obj = DB::table('order_items');
    //                 } else {
    //                     $obj = DB::table('orders');
    //                 }
    //             } else {

    //                 if (Schema::hasTable($table)) {
    //                     $obj = DB::table($table);
    //                 } else {
    //                     if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
    //                         $obj = DB::table('order_items');
    //                     } else {
    //                         $obj = DB::table('orders');
    //                     }
    //                 }
    //             }
    //         }
    //     $obj = DB::table('orders');
    //         $where = [];

    //         if ($user_id == "") {
    //             $user_id = $user->user_id;
    //         }
    //         if($user_id==101){
    //             $store = DB::table('stores')->where(array('ascription'=>1))->select('store_id')->get();
    //             $store=json_decode($store,true);
    //             $store_ids=[];
    //             foreach($store as $key=>$val){
    //                 $store_ids[$key]=$val['store_id'];
    //             }
    //             $user_ids = $this->getSubIds(1);
    //         }else{
    //             $user_ids = $this->getSubIds($user_id);
    //         }

    //         if ($amount_start) {
    //             $where[] = ['total_amount', '>=', $amount_start];
    //         }
	    
    //         if ($amount_end) {
    //             $where[] = ['total_amount', '<=', $amount_end];
    //         }
	    
    //         if ($pay_status) {
    //             $where[] = ['pay_status', '=', $pay_status];
    //         }
            
    //         if ($discount==1) {
    //             $where[] = ['discount_amount', '>', 0];
    //         }

    //         if ($device_id) {
    //             $where[] = ['device_id', '=', $device_id];
    //         }
	    
    //         if ($store_id) {
    //             $where[] = ['store_id', '=', $store_id];
    //         }
	    
    //         if ($company) {
    //             $where[] = ['company', '=', $company];
    //         }
	    
    //         if ($ways_source) {
    //             if ($ways_source == 'alipay_face_dis_day') {
    //                 $where[] = ['ways_source', '=', 'alipay_face'];
    //             } elseif ($ways_source == 'weixin_face_dis_day') {
    //                 $where[] = ['pay_method', '=', 'weixin_face'];
    //             } else {
    //                 if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
    //                     $where[] = ['pay_method', '=', $ways_source];
    //                 } else {
    //                     $where[] = ['ways_source', '=', $ways_source];
    //                 }
    //             }
    //         }
	    
    //         if ($ways_type) {
    //             $where[] = ['ways_type', '=', $ways_type];
    //         }
	    
    //         if ($time_start) {
    //             $where[] = ['created_at', '>=', $time_start];
    //         }
	    
    //         if ($time_end) {
    //             $where[] = ['created_at', '<=', $time_end];
    //         }
	    
    //         if ($device_id) {
    //             $where[] = ['device_id', '=', $device_id];
    //         }
    //         if($user_id==101){
    //             if ($sort) {
    //                 $obj = $obj->where($where)
    //                     ->whereIn('user_id', $user_ids)
    //                     ->orderBy('total_amount', $sort);
    //             }
    //             elseif ($out_trade_no || $trade_no) {
    //                 if ($out_trade_no) {
    //                     $where1[] = ['out_trade_no', $out_trade_no];
    //                 }
    
    //                 if ($trade_no) {
    //                     $where1[] = ['trade_no', $trade_no];
    //                 }
    
    //                 $obj = $obj->where($where1)
    //                     ->whereIn('user_id', $user_ids)
    //                     ->orderBy('created_at', 'desc');
    //             }
    //             else {
    //                 $obj = $obj->where($where)
    //                     ->whereIn('user_id', $user_ids)
    //                     ->whereIn('store_id', $store_ids)
    //                     ->orderBy('created_at', 'desc');
    //             }
    //         }else{
    //             if ($sort) {
    //                 $obj = $obj->where($where)
    //                     ->whereIn('user_id', $user_ids)
    //                     ->orderBy('total_amount', $sort);
    //             }
    //             elseif ($out_trade_no || $trade_no) {
    //                 if ($out_trade_no) {
    //                     $where1[] = ['out_trade_no', $out_trade_no];
    //                 }
    
    //                 if ($trade_no) {
    //                     $where1[] = ['trade_no', $trade_no];
    //                 }
    
    //                 $obj = $obj->where($where1)
    //                     ->whereIn('user_id', $user_ids)
    //                     ->orderBy('created_at', 'desc');
    //             }
    //             else {
    //                 $obj = $obj->where($where)
    //                     ->whereIn('user_id', $user_ids)
    //                     ->orderBy('created_at', 'desc');
    //             }
    //         }

    //         $this->t = $obj->count();
    //         $data = $this->page($obj)->get();
    //         $data=json_decode($data,true);
    //         $this->status = 1;
    //         $this->message = '数据返回成功';
    //         foreach ($data as $k=>$v){
    //             $data[$k]['discount_amount']=sprintf("%.2f",$v['discount_amount']);
    //             $data[$k]['actual_amount']=sprintf("%.2f",$v['total_amount']-$v['discount_amount']);
    //         }

    //         return $this->format($data);
    //     } catch (\Exception $exception) {
    //         $this->status = -1;
    //         $this->message = $exception->getMessage();
    //         return $this->format();
    //     }
    // }

    public function orderNew(Request $request){
        try {
            $user = $this->parseToken();
            /**
            $store_id = $request->get('store_id', '');
            $user_id = $request->get('user_id', '');
            $pay_status = $request->get('pay_status', '');
            $ways_source = $request->get('ways_source', '');
            $company = $request->get('company', '');
            $device_id = $request->get('device_id', ''); //设备id
            $sort = $request->get('sort', '');
            $out_trade_no = $request->get('out_trade_no', '');
            $trade_no = $request->get('trade_no', '');
            $time_start = $request->get('time_start', ''); // 2022-04-01 00:00:00
            $time_end = $request->get('time_end', ''); // 2022-04-03 23:59:59
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $total_amount = $request->get("total_amount",-1);
             */
            $orderQueryController=new OrderQueryController();
            $where=[
                "page"=>$request->input("p",1),
                "pagesize"=>$request->input("l",200),
                "user_id"=> $request->input('user_id', ''),
                "store_id"=>$request->input("store_id",""),
                "pay_status"=>$request->input("pay_status",""),
                "ways_source"=>$request->input("ways_source",""),
                "company"=>$request->input("company",""),
                "device_id"=>$request->input("device_id",""),
                "out_trade_no"=>$request->input("out_trade_no",""),
                "trade_no"=>$request->input("trade_no",""),
                "start_date"=>$request->get('time_start', ''),
                "end_date"=>$request->get('time_start', ''),
                "total_amount"=>$request->get('total_amount', 0),
            ];
            $where=$orderQueryController->returnWhere($user,$where);
            $service = new UserService();
//            PrintSqlService::listen();
            $result = $service->orderQuery($where);
            $this->needpage=true;
            $this->l = $where["pagesize"];
            $this->p = $where["page"];
            $this->t = $result["total"];
            $this->status = 1;
            $this->message = '数据返回成功';
            $data=$result["list"];
            foreach ($data as $k=>$v){
                $v['discount_amount']=sprintf("%.2f",$v['discount_amount']);
                $v['actual_amount']=sprintf("%.2f",$v['total_amount']-$v['discount_amount']);
                $data[$k]=$v;
            }
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = '暂无数据'.$exception->getMessage();
            return $this->format();
        }
    }

    public function order(Request $request){
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $user_id = $request->get('user_id', '');
            $pay_status = $request->get('pay_status', '');
            $ways_source = $request->get('ways_source', '');
            $company = $request->get('company', '');
            $device_id = $request->get('device_id', ''); //设备id
            $sort = $request->get('sort', '');
            $out_trade_no = $request->get('out_trade_no', '');
            $trade_no = $request->get('trade_no', '');

            $time_start = $request->get('time_start', ''); // 2022-04-01 00:00:00
            $time_end = $request->get('time_end', ''); // 2022-04-03 23:59:59

            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $total_amount = $request->get("total_amount",-1);
            //想检索金额，必须使用复合条件
            if($total_amount>0 && ($store_id=="")){
                return json_encode([
                    'status' => 2,
                    'message' => "按照金额来查询，必须传入店铺ID"
                ]);
            }

            if (in_array($time_start, [null, ''])) {
                $time_start = $time_start_s;
            }

            if (in_array($time_end, [null, ''])) {
                $time_end = $time_start_e;
            }

            $re_data = [
                'time_start' => $time_start,
                'time_end' => $time_end,
            ];
            $check_data = [
                'time_start' => '订单开始时间',
                'time_end' => '订单结束时间',
            ];
            $check = $this->check_required($re_data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }
            if($time_start>$time_end){
                return json_encode([
                    'status' => 2,
                    'message' => '开始时间不能大于结束时间'
                ]);
            }

            // 限制时间
            $date = ( strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => '时间跨度不能超过'.$day.'天'
                ]);
            }
            
            if( $time_end >= date('Y-m-d 23:59:59', time()) ){
                $time_end = date('Y-m-d 23:59:59', time());
            }

            //跨天操作
            $time_start_db = date('Ymd', strtotime($time_start));
            $time_end_db = date('Ymd', strtotime($time_end));;
            $is_ct_time = 0;
            if ($time_start_db != $time_end_db) { // 判断是否跨天
                $is_ct_time = 1;
            }

            $day = date('Ymd', strtotime($time_start));
            $table = 'orders_' . $day;
            
            $where = [];
            if ($user_id == "") {
                $user_id = $user->user_id;
                
            }
            if($user_id==101){
                $store = DB::table('stores')->where(array('ascription'=>1))->select('store_id')->get();
                $store=json_decode($store,true);
                $store_ids=[];
                foreach($store as $key=>$val){
                    $store_ids[$key]=$val['store_id'];
                }
                $user_ids = $this->getSubIds(1);
            } else if ($user_id == 747) {
                $store = DB::table('stores')->where(array('ascription'=>4))->select('store_id')->get();
                $store=json_decode($store,true);
                $store_ids=[];
                foreach($store as $key=>$val){
                    $store_ids[$key]=$val['store_id'];
                }
                $user_ids = $this->getSubIds(1);
            } else if ($user_id == 874) {
                 $store = DB::table('stores')->where(array('ascription'=>5))->select('store_id')->get();
                $store=json_decode($store,true);
                $store_ids=[];
                foreach($store as $key=>$val){
                    $store_ids[$key]=$val['store_id'];
                }
                $user_ids = $this->getSubIds(1);
            } else if ($user_id == 1002) {
                $store = DB::table('stores')->where(array('ascription'=>6))->select('store_id')->get();
                $store=json_decode($store,true);
                $store_ids=[];
                foreach($store as $key=>$val){
                    $store_ids[$key]=$val['store_id'];
                }
                $user_ids = $this->getSubIds(1);
            }else{
                $user_ids = $this->getSubIds($user_id); // 所有下级代理id
            }

            if ($pay_status) {
                $where[] = ['pay_status', '=', $pay_status];
            }

            if ($device_id) {
                $where[] = ['device_id', '=', $device_id];
            }
	    
            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];
            }
	    
            if ($company) {
                $where[] = ['company', '=', $company];
            }

            if($total_amount>0){
                $where[] = ['total_amount', '=', $total_amount];
            }

            if ($ways_source) {
                if ($ways_source == 'alipay_face_dis_day') {
                    $where[] = ['ways_source', '=', 'alipay_face'];
                } elseif ($ways_source == 'weixin_face_dis_day') {
                    $where[] = ['pay_method', '=', 'weixin_face'];
                } else {
                    if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
                        $where[] = ['pay_method', '=', $ways_source];
                    } else {
                        $where[] = ['ways_source', '=', $ways_source];
                    }
                }
            }
	    
            if ($time_start) {
                $where[] = ['created_at', '>=', $time_start];
            }
	    
            if ($time_end) {
                $where[] = ['created_at', '<=', $time_end];
            }

            if( $time_start >= date('Y-m-d 00:00:00', time()) ) { // 今天
                $obj = DB::table('orders');
            }else{
                if( $is_ct_time ){
                    $test = '';
                    $time = $this->getDateFromRanges( date('Y-m-d', strtotime($time_start)), date('Y-m-d', strtotime($time_end)) );
                    $toEnd = count($time) - 1;
                    // print_r( date('Y-m-d', strtotime($time_start)) );die;
                    foreach( $time as $k => $v ){
                        $where = [];
                        if ($time_start) {
                            if($k==0){
                                $where[] = ['created_at', '>=', $v.' '.date('H:i:s', strtotime($time_start))];
                            }else{
                                $where[] = ['created_at', '>=', $v.' 00:00:00'];
                            }
                        }
            	       // print_r( $where );die;
                        if ($time_end) {
                            if($k == $toEnd){
                                $where[] = ['created_at', '<=', $v.' '.date('H:i:s', strtotime($time_end))];
                            }else{
                                $where[] = ['created_at', '<=', $v.' 23:59:59'];
                            }
                        }
            	    
                        if ($pay_status) {
                            $where[] = ['pay_status', '=', $pay_status];
                        }
            
                        if ($device_id) {
                            $where[] = ['device_id', '=', $device_id];
                        }
            	    
                        if ($store_id) {
                            $where[] = ['store_id', '=', $store_id];
                        }
            	    
                        if ($company) {
                            $where[] = ['company', '=', $company];
                        }
            	    
                        if ($ways_source) {
                            if ($ways_source == 'alipay_face_dis_day') {
                                $where[] = ['ways_source', '=', 'alipay_face'];
                            } elseif ($ways_source == 'weixin_face_dis_day') {
                                $where[] = ['pay_method', '=', 'weixin_face'];
                            } else {
                                if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
                                    $where[] = ['pay_method', '=', $ways_source];
                                } else {
                                    $where[] = ['ways_source', '=', $ways_source];
                                }
                            }
                        }
                        
                        if ($out_trade_no) {
                            $where[] = ['out_trade_no', '=', $out_trade_no];
                        }
                        // print_r($where_users);die;
                        
                        $where_users = '';
                        foreach( $user_ids as $key => $val ){
                            if($key == 0){
                                $where_users = "'" . $val . "'";
                            }else{
                                $where_users = $where_users . ", '" . $val . "'";
                            }
                        }
                        $where[] = [ 'user_id', ' in ', '('.$where_users.')' ];
                        // print_r( $where );die;
                        $sfhh='';
                        foreach ($where as $key=>$val){
                            if( $val[0] == 'user_id' ){
                                if($key == 0){
                                    $sfhh = $sfhh.' where `'.$val[0].'`'.$val[1].$val[2];
                                }else{
                                    $sfhh = $sfhh.' and `'.$val[0].'`'.$val[1].$val[2];
                                }
                            }else{
                                if($key == 0){
                                    $sfhh = $sfhh.' where `'.$val[0].'`'.$val[1]."'".$val[2]."'";
                                }else{
                                    $sfhh = $sfhh.' and `'.$val[0].'`'.$val[1]."'".$val[2]."'";
                                }
                            }
                        }
                        
                        if( $v == date('Y-m-d', time()) ){ // 今天
                            $day_start = 'orders';
                        }else{
                            $day_start = 'orders_' . date( 'Ymd', strtotime($v) );
                        }
                        if( $k == 0 ){
                            $test='(select * from `'.$day_start.'`'.$sfhh.' )';
                            $count = '(select count(*) from `'.$day_start.'`'.$sfhh.')';
                        }else{
                            $test = $test.' union all '.'(select * from `'.$day_start.'`'.$sfhh.' )';
                            $count = $count . ' union all '.'(select count(*) from `'.$day_start.'`'.$sfhh.')';
                        }
                    }

                    $start = abs((abs(trim($request->get('p', 1))) - 1) * abs(trim($request->get('l', 25))));
                    $l=abs(trim($request->get('l', 25)));

                    $test=$test . ' order by `created_at` desc limit '.$start.",".$l;
                    // print_r($test);die;
                    
                    //->offset($start)->limit($this->l)
                    $data = DB::select($test);
                    $count = DB::select($count);
                    // print_r($count);die;

                    $data=json_decode(json_encode($data,true),true);
                    $count = json_decode( json_encode($count, true), true );
                    $counts = 0;
                    foreach($count as $k => $v){
//                        print_r($v['count(*)']);die;
                        $counts = $counts + $v['count(*)'];
//                        print_r($counts);die;
                    }
//                    print_r($counts);die;

                    $this->status = 1;
                    $this->message = '数据返回成功';
                    foreach ($data as $k => $v){
                        $data[$k]['discount_amount']=sprintf("%.2f",$v['discount_amount']);
                        $data[$k]['actual_amount']=sprintf("%.2f",$v['total_amount']-$v['discount_amount']);
                        $data[$k]['refund_time'] = $this->refund($v['out_trade_no']);
                    }
                    $this->needpage = 1;
                    $this->t = $counts;

                    return $this->format($data);
                }else{
                    $obj = DB::table($table);
                }
            }
//            print_r($table);die;

            if($user_id==101 || $user_id==874){
                if ($sort) {
                    $obj = $obj->where($where)
                        ->whereIn('user_id', $user_ids)
                        ->orderBy('total_amount', $sort);
                }
                elseif ($out_trade_no || $trade_no) {
                    if ($out_trade_no) {
                        $where1[] = ['out_trade_no', $out_trade_no];
                    }

                    if ($trade_no) {
                        $where1[] = ['trade_no', $trade_no];
                    }

                    $obj = $obj->where($where1)
                        ->whereIn('user_id', $user_ids)
                        ->orderBy('created_at', 'desc');
                }
                else {
                    $obj = $obj->where($where)
                        ->whereIn('user_id', $user_ids)
                        ->whereIn('store_id', $store_ids)
                        ->orderBy('created_at', 'desc');
                }
            }else{
                if ($sort) {
                    $obj = $obj->where($where)
                        ->whereIn('user_id', $user_ids)
                        ->orderBy('total_amount', $sort);
                }
                elseif ($out_trade_no || $trade_no) {
                    if ($out_trade_no) {
                        $where1[] = ['out_trade_no', $out_trade_no];
                    }

                    if ($trade_no) {
                        $where1[] = ['trade_no', $trade_no];
                    }

                    $obj = $obj->where($where1)
                        ->whereIn('user_id', $user_ids)
                        ->orderBy('created_at', 'desc');
                }
                else {
                    $obj = $obj->where($where)
                        ->whereIn('user_id', $user_ids)
                        ->orderBy('created_at', 'desc');
                }
            }

            $this->t = $obj->count();
            $data = $this->page($obj)->get();
//            print_r($data);die;
            $data=json_decode($data,true);
            $this->status = 1;
            $this->message = '数据返回成功';
            foreach ($data as $k=>$v){
                $data[$k]['discount_amount']=sprintf("%.2f",$v['discount_amount']);
                $data[$k]['actual_amount']=sprintf("%.2f",$v['total_amount']-$v['discount_amount']);
                $data[$k]['refund_time'] = $this->refund($v['out_trade_no']);
            }
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = '暂无数据';
            return $this->format();
        }
    }

    public function refund ($out_trade_no)
    {
        return DB::table('refund_orders')->where(['out_trade_no' => $out_trade_no])->value('created_at');
    }
    
    // 获取俩时间间隔日期
    public function getDateFromRanges($startdate, $enddate){
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);

        $days = ($etimestamp - $stimestamp) / 86400; // 计算日期段内有多少天
//        print_r( $days );die;
        $date = array(); // 保存每天日期

        for($i = 0; $i <= $days; $i++){
            $date[] = date( 'Y-m-d', $stimestamp + (86400 * $i) );
        }
        return $date;
    }

    // 获取俩时间间隔日期
    public function getDateFromRange($startdate, $enddate){
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);

        $days = ($etimestamp - $stimestamp) / 86400; // 计算日期段内有多少天
        $date = array(); // 保存每天日期

        for($i = 0; $i < $days; $i++){
            $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
        }
        return $date;
    }
    
    /**
     *
     * 2022-07-07 17:37
     */
    function lastbill(Request $request){

        try {
            $user_id = $this->parseToken()->user_id;
//            $user_ids = $this->getSubIds($user_id);
            $store_name = $request->get('store_name', '');
//            print_r($store_name);die;
            $time_start = date('Y-m-01', strtotime('-1 month'));
            $time_end = date('Y-m-t', strtotime('-1 month'));
            // print_r($time_start);
            // print_r($time_end);die;

//            $store = DB::table('stores')->whereIn('user_id', $user_ids)->select('store_id')->get();
////        $obj = DB::table('stores')->whereIn('user_id', $user_ids);
////        $this->t = $obj->count();
////        $store = $this->page($obj)->select('store_id', 'store_name')->get();
//            $store = json_decode($store, true);
//            $store = array_column($store, 'store_id');

//            print_r($store);die;

            $where = [];
            $where[] = [ 'c.settling_day', '>=', $time_start ];
            $where[] = [ 'c.settling_day', '<=', $time_end ];

            if( $store_name !== ''){
                $where[] = [ 'stores.store_name', 'like', '%' . $store_name . '%' ];
            }
//            print_r($where);die;

            $obj = DB::table('company_day_commission as c')->where($where)->where('c.user_id', $user_id)->leftJoin('stores', 'c.store_id', 'stores.store_id');
            $this->t = $obj->count();
            if($this->t<1){
                $this->status = -1;
                $this->message = '没有数据';
                return $this->format();
            }
            $data = $this->page($obj)->select('c.store_id', 'stores.store_name', 'c.company', 'c.total_amount')->get();
            $data = json_decode($data, true);

            foreach($data as $k => $v){
                $data[$k]['company'] = $this->get_commany_name( $data[$k]['company'] );
            }

//            dump($data);die;

//            foreach($store as $k => $v){
//                print_r($v['store_id']);die;
//                $data[$k]['store_name'] = $v['store_name'];
//                $data[$k]['refund_amount'] = DB::table('stores_day_count')->where($where)->where([ 'store_id' => $v['store_id'] ])->sum('refund_amount');
//                $data[$k]['store_order_count'] = DB::table('stores_day_count')->where($where)->where([ 'store_id' => $v['store_id'] ])->sum('store_order_count');
//                $data[$k]['refund_order_num'] = DB::table('stores_day_count')->where($where)->where([ 'store_id' => $v['store_id'] ])->sum('refund_order_num');
//            }
//            print_r($data);die;
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    /**
     * 上周商户流水
     * 2022-07-12 10:19
     */
    function merchant_turnover_last_week(Request $request){
        try {
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            $store_name = $request->get('store_name', '');
            $user = $this->parseToken();
//            print_r($store_name);die;

            if( $store_name !== ''){
                $wheres[] = [ 'store_name', 'like', '%' . $store_name . '%' ];
            }
//            print_r($store_name);die;

            if( empty($time_start) || empty($time_end) ){
                $this->status = -1;
                $this->message = '请选择时间';
                return $this->format();
            }

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => "时间筛选不能超过 $day 天"
                ]);
            }

            if( empty($time_end) ){
                $time_end = date('Y-m-d H:i:s', time());
            } else {
                if ($time_start != $time_end) {
                    $date = ((strtotime($time_end) > strtotime($time_start)))?true:false;
                    if (!$date) {
                        return json_encode([
                            'status' => 2,
                            'message' => '开始时间不能超过结束时间'
                        ]);
                    }
                }
            }

//            $where[] = ['settling_day', '>=', date('Y-m-d', strtotime('last week'))];
//            $where[] = ['settling_day', '<=', date('Y-m-d', strtotime('this week -1 day'))];
//            $wheres[] = ['created_at', '>=', date('Y-m-d' . ' 00:00:00', strtotime('last week'))];
//            $wheres[] = ['created_at', '<=', date('Y-m-d' . ' 23:59:59', strtotime('this week -1 day'))];


            $where[] = ['settling_day', '>=', $time_start];
            $where[] = ['settling_day', '<=', $time_end];
            $wheres[] = ['created_at', '>=', $time_start . ' 00:00:00'];
            $wheres[] = ['created_at', '<=', $time_end . ' 23:59:59'];
//            print_r($wheres);die;

//            $where[] = ['settling_day', '>=', date('Y-05-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01')))];
//            $where[] = ['settling_day', '<=', date('Y-05-d', strtotime(date('Y-m', time()) . '-01') - 86400)];
//            $wheres[] = ['created_at', '>=', date('Y-05-d' . ' 00:00:00', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')))];
//            $wheres[] = ['created_at', '<=', date('Y-05-d' . ' 23:59:59', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400)];
//            print_r($wheres);die;

            $obj = DB::table('stores')->where('pid', '=', 0)->where($wheres);
            $this->t = $obj->count();
            $stores = $this->page($obj)->select('store_id')->get();
            $stores = json_decode($stores, true);
            $stores = array_column($stores, 'store_id');
//            print_r($stores);die;

            foreach($stores as $k => $v){
//                print_r($v);die;
                $p_stores = DB::table('stores')->where('pid', $v)->select('store_id')->get(); // 查找总店下的分店
//                    print_r($p_stores);die;
                if( !empty( $p_stores[0]->store_id ) ){ // 合并总店和分店
                    $store_ids[0] = $v;
                    $p_stores = json_decode($p_stores, true);
                    foreach($p_stores as $k1 => $v1){
                        $store_ids[] = $v1['store_id'];
                    }
                }else{
                    $store_ids[0] = $v;
                }
//                print_r($store_ids);die;

                $store_pay_ways[$k] = DB::table('store_pay_ways')->where('store_id', $v)->select('company')->distinct()->get();
//                print_r($store_pay_ways);die;
                foreach($store_pay_ways[$k] as $key => $val){
                    $total_amount = DB::table('company_day_commission')->whereIn('store_id', $store_ids)->where([ 'user_id' => $user->user_id ])->where([ 'company' => $val->company ])->where($where)->sum('total_amount');
                    $store_name = DB::table('stores')->where('store_id', $v)->select('store_name')->get('store_name');

                    $arr = array(
                        'store_id' => $v,
                        'store_name' => $store_name[0]->store_name,
                        'company' => $this->get_commany_name( $val->company ),
                        'total_amount' => $total_amount
                    );
                    $data[] = $arr;
                }
            }
//            print_r(2);die;
            if( !isset($data) ){
                $this->status = -1;
                $this->message = '暂无数据';
                return $this->format();
            }

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    /**
     * 通联对账汇总
     * 2022-07-12 10:19
     */
    function allinpayah_count(Request $request){
        try {
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            $store_name = $request->get('store_name', '');
            $user = $this->parseToken();

            if( $store_name !== ''){
                $wheres[] = [ 'store_name', 'like', '%' . $store_name . '%' ];
            }
            $store_like=DB::table('stores')->where($wheres)->select('store_id')->get(); //
            $store_like = json_decode($store_like, true);
            $store_like = array_column($store_like, 'store_id');

            if( empty($time_start) || empty($time_end) ){
                $this->status = -1;
                $this->message = '请选择时间';
                return $this->format();
            }

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => "时间筛选不能超过 $day 天"
                ]);
            }

            if( empty($time_end) ){
                $time_end = date('Y-m-d H:i:s', time());
            } else {
                if ($time_start != $time_end) {
                    $date = ((strtotime($time_end) > strtotime($time_start)))?true:false;
                    if (!$date) {
                        return json_encode([
                            'status' => 2,
                            'message' => '开始时间不能超过结束时间'
                        ]);
                    }
                }
            }
            if(empty($store_like)){
                $obj = DB::table('mechanism_merchant');
            }else{
                $obj = DB::table('mechanism_merchant')->whereIn('merchant_id',$store_like);
            }
            $this->t = $obj->count();
            $stores = $this->page($obj)->select('merchant_id')->get();
            $stores = json_decode($stores, true);
            $stores = array_column($stores, 'merchant_id');

            foreach($stores as $k => $v){
                $store_name = DB::table('stores')->where('store_id', $v)->select('store_name','id')->first(); //
                $p_stores = DB::table('stores')->where('pid', $store_name->id)->select('store_id')->get(); // 查找总店下的分店
                if( !empty( $p_stores[0]->store_id ) ){ // 合并总店和分店
                    $store_ids[0] = $v;
                    $p_stores = json_decode($p_stores, true);
                    foreach($p_stores as $k1 => $v1){
                        $store_ids[] = $v1['store_id'];
                    }
                }else{
                    $store_ids[0] = $v;
                }
                
                $where[] = ['created_at', '>=', $time_start.' 00:00:00'];
                $where[] = ['created_at', '<=', $time_end.' 23:59:59'];
                
                $company_config=DB::table('company_config')->where('model', '2')->whereIn('store_id', $store_ids)->select('id','company')->get(); //
                foreach($company_config as $k1=>$v1){
                    if($v1->id=='3750' ||$v1->id=='3749'){
                        continue;
                    }
                    $total_amount = Db::table('orders')->where($where)->where('company', $v1->company)->whereIn('store_id', $store_ids)->whereIn('pay_status', [1])->select('total_amount')->sum('total_amount');
                     $total_count = Db::table('orders')->where($where)->where('company', $v1->company)->whereIn('store_id', $store_ids)->whereIn('pay_status', [1])->select('total_amount')->count('total_amount');
                    $fee_amount = Db::table('orders')->where($where)->where('company', $v1->company)->whereIn('store_id', $store_ids)->whereIn('pay_status', [1])->select('fee_amount')->sum('fee_amount');
                    $arr = array(
                        'store_id' => $v,
                        'store_name' => $store_name->store_name,
                        'company' => $this->get_commany_name( $v1->company),
                        'total_amount' => $total_amount,
                        'fee_amount' => $fee_amount,
                        'total_count' => $total_count
                    );
                    $data[] = $arr;
                }
                unset($store_ids);
            }
            if( !isset($data) ){
                $this->status = -1;
                $this->message = '暂无数据';
                return $this->format();
            }

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    public function recursion1($user_id,$i = 0){
        $user = DB::table('users')->where([ 'id' => $user_id ])->select('id', 'name', 'pid','level','name')->get();
        $user = json_decode($user,true);
        if( $user[0]['pid'] !== '0'){
            $_SESSION['users'][$i] = $user[0];
            $i++;
            $this->recursion1($user[0]['pid'],$i);
        }else{
            $_SESSION['users'][$i] = $user[0];
        }
        return json_encode((object)$_SESSION['users']);
    }
    /**
     * 渠道查看商户情况及导出
     * 2022-07-12 10:19
     */
    function user_stores(Request $request){
        try {
            session_start();
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            $store_name = $request->get('store_name', '');
            $user = $this->parseToken();

            if( $store_name !== ''){
                $wheres[] = [ 'store_name', 'like', '%' . $store_name . '%' ];
                $store_like=DB::table('stores')->where($wheres)->select('store_id')->get(); //
                $store_like = json_decode($store_like, true);
                $store_like = array_column($store_like, 'store_id');
            }else{
                $store_like=array();
            }
            
            if( empty($time_start) || empty($time_end) ){
                $this->status = -1;
                $this->message = '请选择时间';
                return $this->format();
            }

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => "时间筛选不能超过 $day 天"
                ]);
            }

            if( empty($time_end) ){
                $time_end = date('Y-m-d H:i:s', time());
            } else {
                if ($time_start != $time_end) {
                    $date = ((strtotime($time_end) > strtotime($time_start)))?true:false;
                    if (!$date) {
                        return json_encode([
                            'status' => 2,
                            'message' => '开始时间不能超过结束时间'
                        ]);
                    }
                }
            }
            $user_ids = $this->getSubIds($user->user_id);
            if(empty($store_like)){
                $obj = DB::table('stores')->where('pid',0)->whereIn('user_id',$user_ids);
            }else{
                $obj = DB::table('stores')->where('pid',0)->whereIn('store_id',$store_like)->whereIn('user_id',$user_ids);
            }
            $this->t = $obj->count();
            $stores = $this->page($obj)->select('store_id')->get();
            $stores = json_decode($stores, true);
            $stores = array_column($stores, 'store_id');
            foreach($stores as $k => $v){
                $store_name = DB::table('stores')->where('store_id', $v)->select('store_name','store_id','id','user_id')->first(); //
                $store_way=DB::table('store_pay_ways')->where('store_id', $store_name->store_id)->where('ways_source', 'weixin')->select('company')->orderBy('sort','desc')->first(); //
                if(isset($store_way->company)){
                    $company_rate=DB::table('company_config')->where('store_id', $store_name->store_id)->where('company', $store_way->company)->select('configure_channel_fee')->first(); //
                }
                unset($_SESSION['users']);
                $users = $this->recursion1($store_name->user_id);
                $users = json_decode($users, true);
                $users = array_reverse($users);
                foreach($users as $k3=>$v3){
                    if($v3['level']>=$user->level &&$v3['level']!='0'){
                        if($v3['level']=='1'){
                            $level1_name=$v3['name'];
                        }elseif ($v3['level']=='2'){
                            $level2_name=$v3['name'];
                        }elseif ($v3['level']=='3'){
                            $level3_name=$v3['name'];
                        }
                    }else{
                        $level1_name='直营商户';
                    }
                }
                if(!isset($level1_name)){
                    $level1_name='无';
                }
                if(!isset($level2_name)){
                    $level2_name='无';
                }
                if(!isset($level3_name)){
                    $level3_name='无';
                }
                $p_stores = DB::table('stores')->where('pid', $store_name->id)->select('store_id')->get(); // 查找总店下的分店
                if( !empty( $p_stores[0]->store_id ) ){ // 合并总店和分店
                    $store_ids[0] = $v;
                    $p_stores = json_decode($p_stores, true);
                    foreach($p_stores as $k1 => $v1){
                        $store_ids[] = $v1['store_id'];
                    }
                }else{
                    $store_ids[0] = $v;
                }
                
                $where[] = ['settling_day', '>=', $time_start];
                $where[] = ['settling_day', '<=', $time_end];
                
                $store_count=DB::table('company_day_commission')->where($where)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('total_amount')->sum('total_amount'); //
                $total_num=DB::table('company_day_commission')->where($where)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('total_num')->sum('total_num'); //
                $fee_amount=DB::table('company_day_commission')->where($where)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('fee_amount')->sum('fee_amount'); //
                $commission_money=DB::table('company_day_commission')->where($where)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('commission_money')->sum('commission_money'); //
                $arr = array(
                    'store_id' => $v,
                    'store_name' => $store_name->store_name,
                    'total_num' => $total_num,
                    'total_amount' => $store_count,
                    'fee_rate' => isset($company_rate->configure_channel_fee)?$company_rate->configure_channel_fee:'0.00',
                    'fee_amount' => $fee_amount,
                    'commission_money' => $commission_money,
                    'level1_name'=>$level1_name,
                    'level2_name'=>$level2_name,
                    'level3_name'=>$level3_name,
                );
                $data[] = $arr;
                unset($store_ids);
                unset($level1_name);
                unset($level2_name);
                unset($level3_name);
            }
            if( !isset($data) ){
                $this->status = -1;
                $this->message = '暂无数据';
                return $this->format();
            }

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    function merchant_company(Request $request){
        try {
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            $company_id = $request->get('company_id', '');
            $user = $this->parseToken();
            
            if( empty($time_start) || empty($time_end) ){
                $this->status = -1;
                $this->message = '请选择时间';
                return $this->format();
            }

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => "时间筛选不能超过 $day 天"
                ]);
            }

            if( empty($time_end) ){
                $time_end = date('Y-m-d H:i:s', time());
            } else {
                if ($time_start != $time_end) {
                    $date = ((strtotime($time_end) > strtotime($time_start)))?true:false;
                    if (!$date) {
                        return json_encode([
                            'status' => 2,
                            'message' => '开始时间不能超过结束时间'
                        ]);
                    }
                }
            }
            $user_ids = $this->getSubIds($user->user_id);
            $obj = DB::table('stores')->where('pid',0)->whereIn('user_id',$user_ids);
            $stores = $obj->select('store_id')->get();
            $stores = json_decode($stores, true);
            $stores = array_column($stores, 'store_id');
            foreach($stores as $k => $v){
                $store_name = DB::table('stores')->where('store_id', $v)->select('store_name','store_id','id','user_id')->first(); //
                $p_stores = DB::table('stores')->where('pid', $store_name->id)->select('store_id')->get(); // 查找总店下的分店
                if( !empty( $p_stores[0]->store_id ) ){ // 合并总店和分店
                    $store_ids[0] = $v;
                    $p_stores = json_decode($p_stores, true);
                    foreach($p_stores as $k1 => $v1){
                        $store_ids[] = $v1['store_id'];
                    }
                }else{
                    $store_ids[0] = $v;
                }
                
                $date=$this->getDateFromRanges($time_start,$time_end);
                foreach($date as $key=>$val){
                    $where[] = ['settling_day', '>=', $val];
                    $where[] = ['settling_day', '<=', $val];
                    if(empty($company_id)){
                        $company_config=DB::table('company_config')->whereIn('store_id', $store_ids)->select('company')->distinct()->get(); //
                       foreach ($company_config as $key1=>$val1){
                           $company_rate=DB::table('company_config')->whereIn('store_id', $store_ids)->where('company', $val1->company)->select('configure_channel_fee')->first(); //
                           $company_names=$this->get_commany_name($val1->company);
                        if(!empty($company_rate)){
                            $store_count=DB::table('company_day_commission')->where($where)->where('company', $val1->company)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('total_amount')->sum('total_amount'); //
                            $total_num=DB::table('company_day_commission')->where($where)->where('company', $val1->company)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('total_num')->sum('total_num'); //
                            $fee_amount=DB::table('company_day_commission')->where($where)->where('company', $val1->company)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('fee_amount')->sum('fee_amount'); //
                            $commission_money=DB::table('company_day_commission')->where($where)->where('company', $val1->company)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('commission_money')->sum('commission_money'); //
                            $arr = array(
                                'store_id' => $v. "\t",
                                'store_name' => $store_name->store_name,
                                'date' => $val,
                                'company_names' => $company_names,
                                'total_num' => $total_num,
                                'total_amount' => $store_count,
                                'fee_rate' => isset($company_rate->configure_channel_fee)?$company_rate->configure_channel_fee:'0.00',
                                'fee_amount' => $fee_amount,
                                'commission_money' => $commission_money,
                            );
                            $data[] = $arr;
                        }
                       }
                    }else{
                        $company_rate=DB::table('company_config')->whereIn('store_id', $store_ids)->where('company', $company_id)->select('configure_channel_fee')->first(); //
                        $company_names=$this->get_commany_name($company_id);
                        if(!empty($company_rate)){
                            $store_count=DB::table('company_day_commission')->where($where)->where('company', $company_id)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('total_amount')->sum('total_amount'); //
                            $total_num=DB::table('company_day_commission')->where($where)->where('company', $company_id)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('total_num')->sum('total_num'); //
                            $fee_amount=DB::table('company_day_commission')->where($where)->where('company', $company_id)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('fee_amount')->sum('fee_amount'); //
                            $commission_money=DB::table('company_day_commission')->where($where)->where('company', $company_id)->where('user_id', $user->user_id)->whereIn('store_id', $store_ids)->select('commission_money')->sum('commission_money'); //
                            
                            $arr = array(
                                'store_id' => $v. "\t",
                                'store_name' => $store_name->store_name,
                                'date' => $val,
                                'company_names' => $company_names,
                                'total_num' => $total_num,
                                'total_amount' => $store_count,
                                'fee_rate' => isset($company_rate->configure_channel_fee)?$company_rate->configure_channel_fee:'0.00',
                                'fee_amount' => $fee_amount,
                                'commission_money' => $commission_money,
                            );
                            $data[] = $arr;
                        }
                    }
                    unset($where);
                }
                unset($store_ids);
            }
            if( !isset($data) ){
                $this->status = -1;
                $this->message = '暂无数据';
                return $this->format();
            }

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    public function order_info(Request $request)
    {
        try {
            $user = $this->parseToken();
            $out_trade_no = $request->get('out_trade_no', '');
            $a = array('a', '_', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $b = str_ireplace($a, "", $out_trade_no);
            $day = substr($b, 0, 8);
            $table = 'orders_' . $day;

            if (Schema::hasTable($table)) {
                $data = DB::table($table)->where('out_trade_no', $out_trade_no)->first();

            } else {
                if (Schema::hasTable('order_items')) {
                    $data = DB::table('order_items')->where('out_trade_no', $out_trade_no)->first();
                } else {
                    $data = DB::table('orders')->where('out_trade_no', $out_trade_no)->first();
                }
            }


            if (!$data) {
                $this->status = 2;
                $this->message = '订单号不存在';
                return $this->format();
            }
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //对账统计-比较全-实时
    public function order_count(Request $request)
    {
        // dump(2);die;
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $user_id = $request->get('user_id', '');
            $ways_source = $request->get('ways_source', '');
            $company = $request->get('company', '');
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $device_id=$request->get('device_id', '');
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');

            if (in_array($time_start, [null, ''])) {
                $time_start = $time_start_s;
            }

            if (in_array($time_end, [null, ''])) {
                $time_end = $time_start_e;
            }

            $return_type = $request->get('return_type', '01');
            $amount_start = $request->get('amount_start', '');
            $amount_end = $request->get('amount_end', '');

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;

            $now_time = date('Y-m-d H:i:s', time());
            $end = date('Y-m-d 22:00:00', time());
            if ($now_time > $end) {
                $day = 31;
            }

            //限制时间
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => '时间跨度不能超过'.$day.'天'
                ]);
            }

            //跨天操作
            $time_start_db = date('Ymd', strtotime($time_start));
            $time_end_db = date('Ymd', strtotime($time_end));;
            $is_ct_time = 0;
            if ($time_start_db != $time_end_db) {
                $is_ct_time = 1;
            }

            $check_data = [
                'time_start' => '开始时间',
                'time_end' => '结束时间',
            ];
            $where = [];
            $whereIn = [];
            $store_ids = [];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            //条件查询
            if ($time_start) {
                $time_start = date('Y-m-d H:i:s', strtotime($time_start));
                $where[] = ['bill_date', '>=', strtotime($time_start)];
            }
            if ($time_end) {
                $time_end = date('Y-m-d H:i:s', strtotime($time_end));
                $where[] = ['bill_date', '<=', strtotime($time_end)];
            }
            // if ($device_id) {
            //     $where[] = ['device_id', '=', $device_id];
            // }
            // if ($amount_start) {
            //     $where[] = ['total_amount', '>=', $amount_start];
            // }
            // if ($amount_end) {
            //     $where[] = ['total_amount', '<=', $amount_end];
            // }
            if ($company) {
                $company_ways=DB::table('store_ways_desc')->where('company', $company)->select('ways_type as receive_code')->get();
                $company_ways=json_decode($company_ways,true);
                // $where[] = ['company', $company];
            }
            if ($ways_source) {
                $company_ways=DB::table('store_ways_desc')->where('ways_source', $ways_source)->select('ways_type as receive_code')->get();
                $company_ways=json_decode($company_ways,true);
            }
            if ($store_id) {
                $store1 = DB::table('stores')->where(array('store_id'=>$store_id))->select('id')->first();
                $store_f=DB::table('stores')->where(array('pid'=>$store1->id))->select('store_id')->get();
                if(empty($store_f[0]->store_id)){
                    $store_ids1[0]=$store_id;
                }else{
                    $store_f=json_decode($store_f,true);
                    $store_ids1=[];
                    $store_ids1[0]=$store_id;
                    foreach($store_f as $key=>$val){
                        $store_ids1[$key+1]=$val['store_id'];
                    }
                }
            }else{
                $store_ids1=[];
            }
            if ($user_id) {
                if($user_id==101){
                    $user_ids = $this->getSubIds(1);
                } else if ($user_id == 747) {
                    $store = DB::table('stores')->where(array('ascription'=>4))->select('store_id')->get();
                    $store=json_decode($store,true);
                    $store_ids=[];
                    foreach($store as $key=>$val){
                        $store_ids[$key]=$val['store_id'];
                    }
                    $user_ids = $this->getSubIds(1);
                }else{
                    $user_ids = $this->getSubIds($user_id);
                }
            } else {
                if($user->user_id==101){
                    $user_ids = $this->getSubIds(1);
                } else if ($user->user_id == 747) {
                    $store = DB::table('stores')->where(array('ascription'=>4))->select('store_id')->get();
                    $store=json_decode($store,true);
                    $store_ids=[];
                    foreach($store as $key=>$val){
                        $store_ids[$key]=$val['store_id'];
                    }
                    $user_ids = $this->getSubIds(1);
                }else{
                    $user_ids = $this->getSubIds($user->user_id);
                }
            }
            //区间
            $e_order = '0.00';

            $day = date('Ymd', strtotime($time_start));
            $table = 'orders_' . $day;

            $obj = DB::table('orders');
            $obj1 = DB::table('orders');
            
            if($user_id==101 ||$user->user_id==101){
                $order_data = $obj->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                ->where($where)
                ->whereIn('pay_status', [1, 6])//成功+退款
                ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');

                $refund_obj = $obj1->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', '=', 6) //退款
                    ->select('refund_amount');
    
                $member_data = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('pay_status', 1)
                    //->where('pay_status_desc', '会员卡支付成功') // 会员卡支付成功
    		        ->where('ways_type', '18888') //会员卡支付
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                $discount_obj = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->where('discount_amount', '>', 0) //退款
                    ->select('discount_amount');
            }else{
                if($company ||$ways_source){
                    if ($store_id) {
                if (date('Ym', strtotime($time_end)) - date('Ym', strtotime($time_start)) > 0) {
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $table1='wft_stores_orders_' . date('Ym', strtotime($time_end)) . '_bill';
                    $ways_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['order_amount']);
                    $ways_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['order_amount']);
                    $ways_amount->unionAll($ways_amount1);
                    $total_amount=0;
                    foreach ($ways_amount->get() as $obj) {
                        $total_amount += $obj->order_amount;
                    }
                    $total_amount=$total_amount/100;
                    
                    $refund_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['refund_amount']);
                    $refund_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['refund_amount']);
                    $refund_amount_zong->unionAll($refund_amount1);
                    $refund_amount=0;
                    foreach ($refund_amount_zong->get() as $obj) {
                        $refund_amount += $obj->refund_amount;
                    }
                    $refund_amount=$refund_amount/100;
                    
                    $fee_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['receive_fee']);
                    $fee_amount_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['receive_fee']);
                    $fee_amount_zong->unionAll($fee_amount_zong1);
                    $fee_amount=0;
                    foreach ($fee_amount_zong->get() as $obj) {
                        $fee_amount += $obj->receive_fee;
                    }
                    $fee_amount=$fee_amount/100;
                    
                    $total_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['order_total']);
                    $total_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['order_total']);
                    $total_count_zong->unionAll($total_count_zong1);
                    $total_count=0;
                    foreach ($total_count_zong->get() as $obj) {
                        $total_count += $obj->order_total;
                    }
                    
                    $refund_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['refund_total']);
                    $refund_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select(['refund_total']);
                    $refund_count_zong->unionAll($refund_count_zong1);
                    $refund_count=0;
                    foreach ($refund_count_zong->get() as $obj) {
                        $refund_count += $obj->refund_total;
                    }
                }else{
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $total_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select('order_amount')->sum('order_amount')/100;
                    $refund_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select('refund_amount')->sum('refund_amount')/100;
                    $fee_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select('receive_fee')->sum('receive_fee')/100;
                    $total_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select('order_total')->sum('order_total');
                    $refund_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('receive_code', $company_ways)->whereIn('sales_id',$user_ids)->select('refund_total')->sum('refund_total');
                }
                }else{
                if (date('Ym', strtotime($time_end)) - date('Ym', strtotime($time_start)) > 0) {
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $table1='wft_stores_orders_' . date('Ym', strtotime($time_end)) . '_bill';
                    $ways_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['order_amount']);
                    $ways_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['order_amount']);
                    $ways_amount->unionAll($ways_amount1);
                    $total_amount=0;
                    foreach ($ways_amount->get() as $obj) {
                        $total_amount += $obj->order_amount;
                    }
                    $total_amount=$total_amount/100;
                    
                    $refund_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['refund_amount']);
                    $refund_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['refund_amount']);
                    $refund_amount_zong->unionAll($refund_amount1);
                    $refund_amount=0;
                    foreach ($refund_amount_zong->get() as $obj) {
                        $refund_amount += $obj->refund_amount;
                    }
                    $refund_amount=$refund_amount/100;
                    
                    $fee_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['receive_fee']);
                    $fee_amount_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['receive_fee']);
                    $fee_amount_zong->unionAll($fee_amount_zong1);
                    $fee_amount=0;
                    foreach ($fee_amount_zong->get() as $obj) {
                        $fee_amount += $obj->receive_fee;
                    }
                    $fee_amount=$fee_amount/100;
                    
                    $total_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['order_total']);
                    $total_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['order_total']);
                    $total_count_zong->unionAll($total_count_zong1);
                    $total_count=0;
                    foreach ($total_count_zong->get() as $obj) {
                        $total_count += $obj->order_total;
                    }
                    
                    $refund_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['refund_total']);
                    $refund_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select(['refund_total']);
                    $refund_count_zong->unionAll($refund_count_zong1);
                    $refund_count=0;
                    foreach ($refund_count_zong->get() as $obj) {
                        $refund_count += $obj->refund_total;
                    }
                }else{
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $total_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select('order_amount')->sum('order_amount')/100;
                    $refund_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select('refund_amount')->sum('refund_amount')/100;
                    $fee_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select('receive_fee')->sum('receive_fee')/100;
                    $total_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select('order_total')->sum('order_total');
                    $refund_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->whereIn('receive_code', $company_ways)->select('refund_total')->sum('refund_total');
                }
                }
                }else{
                    if ($store_id) {
                if (date('Ym', strtotime($time_end)) - date('Ym', strtotime($time_start)) > 0) {
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $table1='wft_stores_orders_' . date('Ym', strtotime($time_end)) . '_bill';
                    $ways_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['order_amount']);
                    $ways_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['order_amount']);
                    $ways_amount->unionAll($ways_amount1);
                    $total_amount=0;
                    foreach ($ways_amount->get() as $obj) {
                        $total_amount += $obj->order_amount;
                    }
                    $total_amount=$total_amount/100;
                    
                    $refund_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['refund_amount']);
                    $refund_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['refund_amount']);
                    $refund_amount_zong->unionAll($refund_amount1);
                    $refund_amount=0;
                    foreach ($refund_amount_zong->get() as $obj) {
                        $refund_amount += $obj->refund_amount;
                    }
                    $refund_amount=$refund_amount/100;
                    
                    $fee_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['receive_fee']);
                    $fee_amount_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['receive_fee']);
                    $fee_amount_zong->unionAll($fee_amount_zong1);
                    $fee_amount=0;
                    foreach ($fee_amount_zong->get() as $obj) {
                        $fee_amount += $obj->receive_fee;
                    }
                    $fee_amount=$fee_amount/100;
                    
                    $total_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['order_total']);
                    $total_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['order_total']);
                    $total_count_zong->unionAll($total_count_zong1);
                    $total_count=0;
                    foreach ($total_count_zong->get() as $obj) {
                        $total_count += $obj->order_total;
                    }
                    
                    $refund_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['refund_total']);
                    $refund_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select(['refund_total']);
                    $refund_count_zong->unionAll($refund_count_zong1);
                    $refund_count=0;
                    foreach ($refund_count_zong->get() as $obj) {
                        $refund_count += $obj->refund_total;
                    }
                }else{
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $total_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select('order_amount')->sum('order_amount')/100;
                    $refund_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select('refund_amount')->sum('refund_amount')/100;
                    $fee_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select('receive_fee')->sum('receive_fee')/100;
                    $total_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select('order_total')->sum('order_total');
                    $refund_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('shop_id', $store_ids1)->whereIn('sales_id',$user_ids)->select('refund_total')->sum('refund_total');
                }
                }else{
                if (date('Ym', strtotime($time_end)) - date('Ym', strtotime($time_start)) > 0) {
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $table1='wft_stores_orders_' . date('Ym', strtotime($time_end)) . '_bill';
                    $ways_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select(['order_amount']);
                    $ways_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->select(['order_amount']);
                    $ways_amount->unionAll($ways_amount1);
                    $total_amount=0;
                    foreach ($ways_amount->get() as $obj) {
                        $total_amount += $obj->order_amount;
                    }
                    $total_amount=$total_amount/100;
                    
                    $refund_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select(['refund_amount']);
                    $refund_amount1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->select(['refund_amount']);
                    $refund_amount_zong->unionAll($refund_amount1);
                    $refund_amount=0;
                    foreach ($refund_amount_zong->get() as $obj) {
                        $refund_amount += $obj->refund_amount;
                    }
                    $refund_amount=$refund_amount/100;
                    
                    $fee_amount_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select(['receive_fee']);
                    $fee_amount_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->select(['receive_fee']);
                    $fee_amount_zong->unionAll($fee_amount_zong1);
                    $fee_amount=0;
                    foreach ($fee_amount_zong->get() as $obj) {
                        $fee_amount += $obj->receive_fee;
                    }
                    $fee_amount=$fee_amount/100;
                    
                    $total_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select(['order_total']);
                    $total_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->select(['order_total']);
                    $total_count_zong->unionAll($total_count_zong1);
                    $total_count=0;
                    foreach ($total_count_zong->get() as $obj) {
                        $total_count += $obj->order_total;
                    }
                    
                    $refund_count_zong=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select(['refund_total']);
                    $refund_count_zong1=DB::connection('old_mysql')->table($table1)->where($where)->whereIn('sales_id',$user_ids)->select(['refund_total']);
                    $refund_count_zong->unionAll($refund_count_zong1);
                    $refund_count=0;
                    foreach ($refund_count_zong->get() as $obj) {
                        $refund_count += $obj->refund_total;
                    }
                }else{
                    $table='wft_stores_orders_' . date('Ym', strtotime($time_start)) . '_bill';
                    $total_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select('order_amount')->sum('order_amount')/100;
                    $refund_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select('refund_amount')->sum('refund_amount')/100;
                    $fee_amount=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select('receive_fee')->sum('receive_fee')/100;
                    $total_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select('order_total')->sum('order_total');
                    $refund_count=DB::connection('old_mysql')->table($table)->where($where)->whereIn('sales_id',$user_ids)->select('refund_total')->sum('refund_total');
                }
                }
                }
            }
            //总的
            $discount_amount = '0.00';//优惠金额
            $fee_amount = $fee_amount;//结算服务费/手续费
            $mdiscount_amount = '0';// $order_data->sum('mdiscount_amount');//商家优惠金额
            $total_member_amount = '0.00'; //会员卡支付金额
            $get_amount = $total_amount - $refund_amount - $total_member_amount;//商家实收，交易金额-退款金额-会员卡支付金额
            $receipt_amount = $get_amount - $fee_amount;//实际净额，实收-手续费
            $e_order = '' . $e_order . '';
            $total_count = $total_count;
            $refund_count = $refund_count;
            $discount_count=0;


            $data = [
                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                'total_count' => '' . $total_count . '',//交易笔数
                'refund_count' => '' . $refund_count . '',//退款笔数
                'discount_count' => '' . $discount_count . '',//优惠笔数
                'get_amount' => number_format($get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                'receipt_amount' => number_format($receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                'mdiscount_amount' => number_format($mdiscount_amount, 2, '.', ''),
                'member_total_amount' => number_format($total_member_amount, 2, '.', '') //会员支付金额
            ];

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //对账统计-比较全-非实时
    public function order_count1(Request $request){
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $user_id = $request->get('user_id', '');
            if(empty($user_id)){
                $user_id=$user->user_id;
            }
            $ways_source = $request->get('ways_source', '');
            $company = $request->get('company', '');
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $device_id=$request->get('device_id', '');
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            if($time_start>$time_end){
                return json_encode([
                    'status' => 2,
                    'message' => '开始时间不能大于结束时间'
                ]);
            }

            if (in_array($time_start, [null, ''])) {
                $time_start = $time_start_s;
            }

            if (in_array($time_end, [null, ''])) {
                $time_end = $time_start_e;
            }

            $return_type = $request->get('return_type', '01');
            $amount_start = $request->get('amount_start', '');
            $amount_end = $request->get('amount_end', '');

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;

            $now_time = date('Y-m-d H:i:s', time());
            $end = date('Y-m-d 22:00:00', time());
            if ($now_time > $end) {
                $day = 31;
            }

            //限制时间
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => '时间跨度不能超过'.$day.'天'
                ]);
            }

            //跨天操作
            $time_start_db = date('Ymd', strtotime($time_start));
            $time_end_db = date('Ymd', strtotime($time_end));;
            $is_ct_time = 0;
            if ($time_start_db != $time_end_db) {
                $is_ct_time = 1;
            }

            $check_data = [
                'time_start' => '开始时间',
                'time_end' => '结束时间',
            ];
            $where = [];
            $whereIn = [];
            $store_ids = [];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }
            if ($store_id) {
                $store1 = DB::table('stores')->where(array('store_id'=>$store_id))->select('id')->first();
                $store_f=DB::table('stores')->where(array('pid'=>$store1->id))->select('store_id')->get();
                if(empty($store_f[0]->store_id)){
                    $store_ids1[0]=$store_id;
                }else{
                    $store_f=json_decode($store_f,true);
                    $store_ids1=[];
                    $store_ids1[0]=$store_id;
                    foreach($store_f as $key=>$val){
                        $store_ids1[$key+1]=$val['store_id'];
                    }
                }
            }else{
                $store_ids1=[];
            }
            
            if ($user_id) {
                if($user_id==101){
                    $user_ids = $this->getSubIds(1);
                }else{
                    $user_ids = $this->getSubIds($user_id);
                }
            } else {
                if($user->user_id==101){
                    $user_ids = $this->getSubIds(1);
                }else{
                    $user_ids = $this->getSubIds($user->user_id);
                }
            }
            
            $start=date('Y-m-d', strtotime($time_start));
            $end=date('Y-m-d', strtotime($time_end));
            $today=date('Y-m-d', time());
            
            
            if($start!=$today&&$end==$today){
                
                  //条件查询
                if ($time_start) {
                    $time_start1 = date('Y-m-d 00:00:00', time());
                    $where1[] = ['created_at', '>=', $time_start1];
                }
                if ($time_end) {
                    $time_end = date('Y-m-d H:i:s', strtotime($time_end));
                    $where1[] = ['created_at', '<=', $time_end];
                }
                if ($device_id) {
                    $where1[] = ['device_id', '=', $device_id];
                }
                if ($amount_start) {
                    $where1[] = ['total_amount', '>=', $amount_start];
                }
                if ($amount_end) {
                    $where1[] = ['total_amount', '<=', $amount_end];
                }
                if ($company) {
                    $where1[] = ['company', $company];
                }
                if ($ways_source) {
                    if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
                        $where1[] = ['pay_method', '=', $ways_source];
                    } else {
                        $where1[] = ['ways_source', '=', $ways_source];
                    }
                }
                
                
                //区间
                $e_order = '0.00';
    
                $day = date('Ymd', strtotime($time_start));
                $table = 'orders_' . $day;
    
                $obj = DB::table('orders');
                $obj1 = DB::table('orders');
                
                if ($store_id) {
                    
                    if($user_id == 347 || $user->user_id == 347){
                        $order_data = $obj->whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->whereIn('pay_status', [1, 6])//成功+退款
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
    
                        $refund_obj = $obj1->whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
            
                        $member_data = Order::whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        $discount_obj = Order::whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                        
                    }else{
                        
                        $order_data = $obj->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->whereIn('pay_status', [1, 6])//成功+退款
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
    
                        $refund_obj = $obj1->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
            
                        $member_data = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        $discount_obj = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                            ->where($where1)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                    }
                    
                    
                }else{
                    if($user_id == 347 || $user->user_id == 347){
                        $pingan_stores = DB::table('pingan_stores')->where(['type' => 1])->select('store_id')->get();
                        $pingan_stores = json_encode($pingan_stores);
                        $pingan_stores = json_decode($pingan_stores, true);
                        $pingan_stores = array_column($pingan_stores, 'store_id');
                        
                        $order_data = $obj->whereIn('store_id', $pingan_stores)
                            ->where($where1)
                            ->whereIn('pay_status', [1, 6])//成功+退款
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        
                        $refund_obj = $obj1->whereIn('store_id', $pingan_stores)
                            ->where($where1)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
                        
                        $member_data = Order::whereIn('store_id', $pingan_stores)
                            ->where($where1)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        
                        $discount_obj = Order::whereIn('store_id', $pingan_stores)
                            ->where($where1)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                        
                    }else{
                        $order_data = $obj->whereIn('user_id', $user_ids)
                            ->where($where1)
                            ->whereIn('pay_status', [1, 6])//成功+退款
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
        
                        $refund_obj = $obj1->whereIn('user_id', $user_ids)
                            ->where($where1)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
            
                        $member_data = Order::whereIn('user_id', $user_ids)
                            ->where($where1)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                            
                        $discount_obj = Order::whereIn('user_id', $user_ids)
                            ->where($where1)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                    }
                }
                    
                //总的
                $total_amount_today= $order_data->sum('total_amount');//交易金额
                $refund_amount_today = $refund_obj->sum('refund_amount');//退款金额
                $discount_amount_today = $discount_obj->sum('discount_amount');//优惠金额
                $fee_amount_today = $order_data->sum('fee_amount');//结算服务费/手续费
                $mdiscount_amount_today = '0';// $order_data->sum('mdiscount_amount');//商家优惠金额
                $total_member_amount_today = 0; //会员卡支付金额
                $get_amount_today = $total_amount_today - $refund_amount_today - $mdiscount_amount_today - $total_member_amount_today;//商家实收，交易金额-退款金额-会员卡支付金额
                $receipt_amount_today = $get_amount_today - $fee_amount_today;//实际净额，实收-手续费
                $e_order_today = '' . $e_order . '';
                $total_count_today = '' . count($order_data->get()) . '';
                $refund_count_today = count($refund_obj->get());
                $discount_count_today=count($discount_obj->get());
             }

            if($start==$today&&$end==$today){
                //条件查询
                if ($time_start) {
                    $time_start = date('Y-m-d H:i:s', strtotime($time_start));
                    $where[] = ['created_at', '>=', $time_start];
                }
                if ($time_end) {
                    $time_end = date('Y-m-d H:i:s', strtotime($time_end));
                    $where[] = ['created_at', '<=', $time_end];
                }
                if ($device_id) {
                    $where[] = ['device_id', '=', $device_id];
                }
                if ($amount_start) {
                    $where[] = ['total_amount', '>=', $amount_start];
                }
                if ($amount_end) {
                    $where[] = ['total_amount', '<=', $amount_end];
                }
                if ($company) {
                    $where[] = ['company', '=', $company];
                }
                if ($ways_source) {
                    if (in_array($ways_source, ['alipay_face', 'weixin_face'])) {
                        $where[] = ['pay_method', '=', $ways_source];
                    } else {
                        $where[] = ['ways_source', '=', $ways_source];
                    }
                }
                
                
                //区间
                $e_order = '0.00';
    
                $day = date('Ymd', strtotime($time_start));
                $table = 'orders_' . $day;
    
                $obj = DB::table('orders');
                $obj1 = DB::table('orders');
                if($user_id == 101 || $user->user_id == 101){
                    $order_data = $obj->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                    ->where($where)
                    ->whereIn('pay_status', [1, 6])//成功+退款
                    ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
    
                    $refund_obj = $obj1->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                        ->where($where)
                        ->where('pay_status', '=', 6) //退款
                        ->select('refund_amount');
        
                    $member_data = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                        ->where($where)
                        ->where('pay_status', 1)
        		        ->where('ways_type', '18888') //会员卡支付
                        ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                    $discount_obj = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids)
                        ->where($where)
                        ->where('discount_amount', '>', 0) //退款
                        ->select('discount_amount');
                }
                else if($user_id == 347 || $user->user_id == 347){
                    
                    if($store_id){
                        $order_data = $obj->whereIn('store_id', $store_ids1)
                            ->where($where)
                            ->whereIn('pay_status', [1, 6])//成功+退款
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
        
                        $refund_obj = $obj1->whereIn('store_id', $store_ids1)
                            ->where($where)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
            
                        $member_data = Order::whereIn('store_id', $store_ids1)
                            ->where($where)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        $discount_obj = Order::whereIn('store_id', $store_ids1)
                            ->where($where)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                    }else{
                        $pingan_stores = DB::table('pingan_stores')->where(['type' => 1])->select('store_id')->get();
                        $pingan_stores = json_encode($pingan_stores);
                        $pingan_stores = json_decode($pingan_stores, true);
                        $pingan_stores = array_column($pingan_stores, 'store_id');
                        
                        $order_data = $obj->whereIn('store_id', $pingan_stores)
                            ->where($where)
                            ->whereIn('pay_status', [1, 6])//成功+退款
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        
                        $refund_obj = $obj1->whereIn('store_id', $pingan_stores)
                            ->where($where)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
            
                        $member_data = Order::whereIn('store_id', $pingan_stores)
                            ->where($where)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        
                        $discount_obj = Order::whereIn('store_id', $pingan_stores)
                            ->where($where)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                    }
                    
                    
                }
                else{
                    if ($store_id) {
                        $order_data = $obj->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                        ->where($where)
                        ->whereIn('pay_status', [1, 6])//成功+退款
                        ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
        
                        $refund_obj = $obj1->whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                            ->where($where)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
            
                        $member_data = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                            ->where($where)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        $discount_obj = Order::whereIn('user_id', $user_ids)->whereIn('store_id', $store_ids1)
                            ->where($where)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                    }else{
                        $order_data = $obj->whereIn('user_id', $user_ids)
                        ->where($where)
                        ->whereIn('pay_status', [1, 6])//成功+退款
                        ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
        
                        $refund_obj = $obj1->whereIn('user_id', $user_ids)
                            ->where($where)
                            ->where('pay_status', '=', 6) //退款
                            ->select('refund_amount');
            
                        $member_data = Order::whereIn('user_id', $user_ids)
                            ->where($where)
                            ->where('pay_status', 1)
            		        ->where('ways_type', '18888') //会员卡支付
                            ->select('total_amount', 'refund_amount', 'receipt_amount', 'fee_amount', 'mdiscount_amount');
                        $discount_obj = Order::whereIn('user_id', $user_ids)
                            ->where($where)
                            ->where('discount_amount', '>', 0) //退款
                            ->select('discount_amount');
                        }
                }
                //总的
                $total_amount = $order_data->sum('total_amount');//交易金额
                $refund_amount = $refund_obj->sum('refund_amount');//退款金额
                $discount_amount = $discount_obj->sum('discount_amount');//优惠金额
                $fee_amount = $order_data->sum('fee_amount');//结算服务费/手续费
                $mdiscount_amount = '0';// $order_data->sum('mdiscount_amount');//商家优惠金额
                $total_member_amount = $member_data->sum('total_amount'); //会员卡支付金额
                $get_amount = $total_amount - $refund_amount - $mdiscount_amount - $total_member_amount;//商家实收，交易金额-退款金额-会员卡支付金额
                $receipt_amount = $get_amount - $fee_amount;//实际净额，实收-手续费
                $e_order = '' . $e_order . '';
                $total_count = '' . count($order_data->get()) . '';
                $refund_count = count($refund_obj->get());
                $discount_count=count($discount_obj->get());
    
    
                $data = [
                    'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                    'total_count' => '' . $total_count . '',//交易笔数
                    'refund_count' => '' . $refund_count . '',//退款笔数
                    'discount_count' => '' . $discount_count . '',//优惠笔数
                    'get_amount' => number_format($get_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                    'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                    'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                    'receipt_amount' => number_format($receipt_amount, 2, '.', ''),//实际净额，实收-手续费
                    'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                    'mdiscount_amount' => number_format($mdiscount_amount, 2, '.', ''),
                    'member_total_amount' => number_format($total_member_amount, 2, '.', '') //会员支付金额
                ];
                //附加流水详情
                if ($return_type == "02") {
                    $table = 'orders_' . $day;
                    if (env('DB_D1_HOST')) {
                        //有没有跨天
                        if ($is_ct_time) {
                            if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                                $obj3 = DB::connection("mysql_d1")->table('order_items');
                            } else {
                                $obj3 = DB::connection("mysql_d1")->table('orders');
                            }
                        } else {
                            if (Schema::hasTable($table)) {
                                $obj3 = DB::connection("mysql_d1")->table($table);
                            } else {
                                if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                                    $obj3 = DB::connection("mysql_d1")->table('order_items');
                                } else {
                                    $obj3 = DB::connection("mysql_d1")->table('orders');
                                }
                            }
                        }
                    } else {
                        if ($is_ct_time) {
                            if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                                $obj3 = DB::table('order_items');
                            } else {
                                $obj3 = DB::table('orders');
                            }
                        } else {
                            if (Schema::hasTable($table)) {
                                $obj3 = DB::table($table);
                            } else {
                                if (Schema::hasTable('order_items') && $time_start >= "2019-12-21 00:00:00") {
                                    $obj3 = DB::table('order_items');
                                } else {
                                    $obj3 = DB::table('orders');
                                }
                            }
                        }
                    }
                    $obj3 = $obj3->where($where)
                        ->where('store_id',$store_id)
                        ->whereIn('user_id', $user_ids)
                        ->orderBy('updated_at', 'desc');
                    $this->t = $obj3->count();
                    $data['order_list'] = $this->page($obj3)->get();
                }
            }else{
                if ($time_start) {
                    $time_start = date('Y-m-d', strtotime($time_start));
                    $where[] = ['settling_day', '>=', $time_start];
                }
                if ($time_end) {
                    $time_end = date('Y-m-d', strtotime($time_end));
                    $where[] = ['settling_day', '<=', $time_end];
                }
                if ($company) {
                    $where[] = ['company','=', $company];
                }
                if ($store_id) {
                    $store1 = DB::table('stores')->where(array('store_id'=>$store_id))->select('id')->first();
                    $store_f=DB::table('stores')->where(array('pid'=>$store1->id))->select('store_id')->get();
                    if(empty($store_f[0]->store_id)){
                        $store_ids1[0]=$store_id;
                    }else{
                        $store_f=json_decode($store_f,true);
                        $store_ids1=[];
                        $store_ids1[0]=$store_id;
                        foreach($store_f as $key=>$val){
                            $store_ids1[$key+1]=$val['store_id'];
                        }
                    }
                }else{
                    $store_ids1=[];
                }
                
                $pingan_stores = DB::table('pingan_stores')->where(['type' => 1])->select('store_id')->get();
                $pingan_stores = json_encode($pingan_stores);
                $pingan_stores = json_decode($pingan_stores, true);
                $pingan_stores = array_column($pingan_stores, 'store_id');
                
                
                if($start != $today && $end == $today){
                    if($store_id){
                        if ($ways_source) {
                            if($user_id == 347 || $user->user_id == 347){
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $store_ids1)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }
                            }else{
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }
                            }
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }else{
                            if($user_id == 347 || $user->user_id == 347){
                                $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('total_amount')->sum('total_amount');
                                $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('total_num')->sum('total_num');
                                $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('fee_amount')->sum('fee_amount');
                            }else{
                                $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('total_amount')->sum('total_amount');
                                $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('total_num')->sum('total_num');
                                $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('fee_amount')->sum('fee_amount');
                            }
                            
                            $total_amount=$total_amount+$total_amount_today;
                            $total_count=$total_count+$total_count_today;
                            $refund_count=$refund_count+$refund_count_today;
                            $discount_count=$discount_count+$discount_count_today;
                            $refund_amount=$refund_amount+$refund_amount_today;
                            $discount_amount=$discount_amount+$discount_amount_today;
                            $fee_amount=$fee_amount+$fee_amount_today;
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }
                    }else{
                        if ($ways_source) {
                            if($user_id == 347 || $user->user_id == 347){
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }
                            }else{
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                    $total_amount=$total_amount+$total_amount_today;
                                    $total_count=$total_count+$total_count_today;
                                    $refund_count=$refund_count+$refund_count_today;
                                    $discount_count=$discount_count+$discount_count_today;
                                    $refund_amount=$refund_amount+$refund_amount_today;
                                    $discount_amount=$discount_amount+$discount_amount_today;
                                    $fee_amount=$fee_amount+$fee_amount_today;
                                }
                            }
                            
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }else{
                            if($user_id == 347 || $user->user_id == 347){
                                $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('total_amount')->sum('total_amount');
                                $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('total_num')->sum('total_num');
                                $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('fee_amount')->sum('fee_amount');
                                $total_amount=$total_amount+$total_amount_today;
                                $total_count=$total_count+$total_count_today;
                                $refund_count=$refund_count+$refund_count_today;
                                $discount_count=$discount_count+$discount_count_today;
                                $refund_amount=$refund_amount+$refund_amount_today;
                                $discount_amount=$discount_amount+$discount_amount_today;
                                $fee_amount=$fee_amount+$fee_amount_today;
                            }else{
                                $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_amount')->sum('total_amount');
                                $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_num')->sum('total_num');
                                $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('fee_amount')->sum('fee_amount');
                                $total_amount=$total_amount+$total_amount_today;
                                $total_count=$total_count+$total_count_today;
                                $refund_count=$refund_count+$refund_count_today;
                                $discount_count=$discount_count+$discount_count_today;
                                $refund_amount=$refund_amount+$refund_amount_today;
                                $discount_amount=$discount_amount+$discount_amount_today;
                                $fee_amount=$fee_amount+$fee_amount_today;
                            }
                                
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }
                    }
                    
                }else{
                    if($store_id){
                        
                        if ($ways_source) {
                            if($user_id == 347 || $user->user_id == 347){
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                }
                            }else{
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                }
                            }
                                
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }else{
                            if($user_id == 347 || $user->user_id == 347){
                                $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('total_amount')->sum('total_amount');
                                $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('total_num')->sum('total_num');
                                $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id',$store_ids1)->where($where)->select('fee_amount')->sum('fee_amount');
                            }else{
                                $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('total_amount')->sum('total_amount');
                                $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('total_num')->sum('total_num');
                                $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->whereIn('store_id',$store_ids1)->where($where)->select('fee_amount')->sum('fee_amount');
                            }
                                
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }
                    }else{
                        if ($ways_source) {
                            if($user_id == 347 || $user->user_id == 347){
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                }
                            }else{
                                if($ways_source=='weixin'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_total_amount')->sum('wx_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_total_num')->sum('wx_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_refund_num')->sum('wx_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_discount_num')->sum('wx_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_refund_amount')->sum('wx_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_discount_amount')->sum('wx_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('wx_fee_amount')->sum('wx_fee_amount');
                                }else if($ways_source=='alipay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_total_amount')->sum('zfb_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_total_num')->sum('zfb_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_refund_num')->sum('zfb_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_discount_num')->sum('zfb_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_refund_amount')->sum('zfb_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_discount_amount')->sum('zfb_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('zfb_fee_amount')->sum('zfb_fee_amount');
                                }else if($ways_source=='unionpay'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_total_amount')->sum('un_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_total_num')->sum('un_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_refund_num')->sum('un_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_discount_num')->sum('un_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_refund_amount')->sum('un_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_discount_amount')->sum('un_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('un_fee_amount')->sum('un_fee_amount');
                                }else if($ways_source=='wing'){
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_total_amount')->sum('tel_total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_total_num')->sum('tel_total_num');
                                    $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_refund_num')->sum('tel_refund_num');
                                    $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_discount_num')->sum('tel_discount_num');
                                    $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_refund_amount')->sum('tel_refund_amount');
                                    $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_discount_amount')->sum('tel_discount_amount');
                                    $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('tel_fee_amount')->sum('tel_fee_amount');
                                }
                            }
                                
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }else{
                            
                            if($user_id == 347 || $user->user_id == 347){
                                $total_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('total_amount')->sum('total_amount');
                                $total_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('total_num')->sum('total_num');
                                $refund_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id', 1)->whereIn('store_id', $pingan_stores)->where($where)->select('fee_amount')->sum('fee_amount');
                            }else{
                                // $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_amount')->sum('total_amount');
                                // $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_num')->sum('total_num');
                                // $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('refund_num')->sum('refund_num');
                                // $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('discount_num')->sum('discount_num');
                                // $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('refund_amount')->sum('refund_amount');
                                // $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('discount_amount')->sum('discount_amount');
                                // $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('fee_amount')->sum('fee_amount');
                                // if($user_id==1){
                                //     $total_amount=DB::table('stores_day_count')->where($where)->select('store_amount')->sum('store_amount');
                                //     $total_count=DB::table('stores_day_count')->where($where)->select('store_order_count')->sum('store_order_count');
                                // }else{
                                //     $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_amount')->sum('total_amount');
                                //     $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_num')->sum('total_num');
                                // }
                                // if(empty($company)){
                                //     $total_amount=DB::table('stores_day_count')->where($where)->select('store_amount')->sum('store_amount');
                                //     $total_count=DB::table('stores_day_count')->where($where)->select('store_order_count')->sum('store_order_count');
                                // }else{
                                    $total_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_amount')->sum('total_amount');
                                    $total_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('total_num')->sum('total_num');
                                // }
                                $refund_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('refund_num')->sum('refund_num');
                                $discount_count=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('discount_num')->sum('discount_num');
                                $refund_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('refund_amount')->sum('refund_amount');
                                $discount_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('discount_amount')->sum('discount_amount');
                                $fee_amount=DB::table('company_day_commission')->where('user_id',$user_id)->where($where)->select('fee_amount')->sum('fee_amount');
                            }
                                
                            $data = [
                                'total_amount' => number_format($total_amount, 2, '.', ''),//交易金额
                                'total_count' => '' . $total_count . '',//交易笔数
                                'refund_count' => '' . $refund_count . '',//退款笔数
                                'discount_count' => '' . $discount_count . '',//优惠笔数
                                'get_amount' => number_format($total_amount-$refund_amount, 2, '.', ''),//商家实收，交易金额-退款金额
                                'refund_amount' => number_format($refund_amount, 2, '.', ''),//退款金额
                                'discount_amount' => number_format($discount_amount, 2, '.', ''),//优惠金额
                                'receipt_amount' => number_format($total_amount-$refund_amount-$fee_amount, 2, '.', ''),//实际净额，实收-手续费
                                'fee_amount' => number_format($fee_amount, 2, '.', ''),//结算服务费/手续费
                                'mdiscount_amount' => number_format(0.00, 2, '.', ''),
                                'member_total_amount' => number_format(0.00, 2, '.', '') //会员支付金额
                            ];
                        }
                    }
                }
            }
            
            
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //数据对账-比较全-非实时
    public function order_data(Request $request)
    {
        try {
            $token = $this->parseToken();

            //下级账户
            $user_ids = $this->getSubIds($token->user_id);
            $sub_user = count($user_ids);

            $stores = Store::whereIn('user_id', $user_ids)
                ->select('id')
                ->get();
            $sub_store = count($stores);

            if (env('DB_D1_HOST')) {
            	$Order_obj = DB::connection("mysql_d1")->table('orders');
            } else {
                $Order_obj = DB::table('orders');
            }

            $order_data = $Order_obj->whereIn('user_id', $user_ids)
                ->where('created_at', '>=', date('Y-m-d 00:00:00', time()))
                ->whereIn('pay_status', [1, 6]) //成功+已退款
                ->select('total_amount');
            $day_amount = $order_data->sum('total_amount');
            $day_count = count($order_data->get());

            if (env('DB_D1_HOST')) {
                        $Order_obj1 = DB::connection("mysql_d1")->table('orders');
            } else {
                $Order_obj1 = DB::table('orders');
            }

            //昨天数据
            $order_data_o = $Order_obj1->whereIn('user_id', $user_ids)
                ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime("-1 day")))
                ->where('created_at', '<=', date('Y-m-d 23:59:59', strtotime("-1 day")))
                ->whereIn('pay_status', [1, 6])//成功+退款
                ->select('total_amount');
            $old_day_amount = $order_data_o->sum('total_amount');
            $old_day_count = count($order_data_o->get());

            // 上个月的起始时间:
            $data = date('Y-m-d');
            $base = strtotime(date('Y-m', strtotime($data)) . '-01 00:00:01');
            $begin_time = date('Y-m-01 00:00:00', strtotime('-1 month', $base));

            if (env('DB_D1_HOST')) {
                $Order_obj2 = DB::connection("mysql_d1")->table("orders");
            } else {
                $Order_obj2 = DB::table('orders');
            }

            $end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));

            //上个月
            $order_data_mo = $Order_obj2->whereIn('user_id', $user_ids)
                ->where('created_at', '>=', $begin_time)
                ->where('created_at', '<=', $end_time)
                ->whereIn('pay_status', [1, 6]) //成功+退款
                ->select('total_amount');
            $old_month_amount = $order_data_mo->sum('total_amount');
            $old_month_count = count($order_data_mo->get());

            // 本月起始时间:
            $date = date("Y-m-d");
            // 本月第一天
            $first = date('Y-m-01 0:0:0', strtotime($date));
            if (env('DB_D1_HOST')) {
                $Order_obj3 = DB::connection("mysql_d1")->table("orders");
            } else {
                $Order_obj3 = DB::table('orders');
            }

            //本月
            $order_data_m = $Order_obj3->whereIn('user_id', $user_ids)
                ->where('created_at', '>=', $first)
                ->whereIn('pay_status', [1, 6]) //成功+退款
                ->select('total_amount');
            $month_amount = $order_data_m->sum('total_amount');
            $month_count = count($order_data_m->get());

            return json_encode([
                'status' => 1,
                'data' => [
                    'sub_user' => '' . $sub_user . '',
                    'sub_store' => '' . $sub_store . '',
                    'day_amount' => '' . $day_amount . '',
                    'day_count' => '' . $day_count . '',
                    'old_day_amount' => '' . $old_day_amount . '',
                    'old_day_count' => '' . $old_day_count . '',
                    'month_amount' => '' . $month_amount . '',
                    'month_count' => '' . $month_count . '',
                    'old_month_amount' => '' . $old_month_amount . '',
                    'old_month_count' => '' . $old_month_count . '',
                ]

            ]);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //门店数据
    // public function store_order_data1(Request $request)
    // {
    //     try {
    //         $user = $this->parseToken();
    //         $store_id = $request->get('store_id', '');
    //         $d = date('Ymd', strtotime("-1 day"));
    //         $day = $request->get('day', '');
    //         $day = isset($day) ? $day : $d;
    //         $time_start = $request->get('time_start', '');
    //         $time_end = $request->get('time_end', '');
    //         $order_count = $request->get('order_count', 'desc');
    //         $order_money = $request->get('order_money', 'desc');
    //         $user_ids = $this->getSubIds($user->user_id);

    //         $where = [];

    //         if ($day) {
    //             $where[] = ['store_day_all_orders.day', '=', $day];
    //         }


    //         if ($store_id) {
    //             $store_ids = [
    //                 'store_id' => $store_id
    //             ];
    //         } else {

    //             if (env('DB_D1_HOST')) {
    //                 $Store = DB::connection("mysql_d1")->table("stores");
    //             } else {
    //                 $Store = DB::table('stores');
    //             }

    //             $store_ids = $Store->whereIn('user_id', $user_ids)->select('store_id')->get();
    //             if ($store_ids->isEmpty()) {
    //                 $store_ids = [
    //                     'store_id' => '1'
    //                 ];
    //             } else {
    //                 $store_ids = $store_ids->toArray();
    //             }
    //         }


    //         if (env('DB_D1_HOST')) {
    //             $Order_obj = DB::connection("mysql_d1")->table("store_day_all_orders");
    //         } else {
    //             $Order_obj = DB::table('store_day_all_orders');
    //         }

    //         $Order_obj = $Order_obj->join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
    //             ->where($where)
    //             ->whereIn('store_day_all_orders.store_id', $store_ids)
    //             ->orderBy('store_day_all_orders.order_sum', $order_count)
    //             ->orderBy('store_day_all_orders.total_amount', $order_money)
    //             ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
    //         $this->t = $Order_obj->count();
    //         $data = $this->page($Order_obj)->get();
    //         $this->status = 1;
    //         $this->message = '数据返回成功';
    //         return $this->format($data);

    //     } catch (\Exception $exception) {
    //         dd($exception);
    //         $this->status = -1;
    //         $this->message = $exception->getMessage() . $exception->getLine();
    //         return $this->format();
    //     }
    // }


    //门店数据
    public function store_order_data(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $d = date('Ymd', strtotime("-1 day"));
            $day = $request->get('day', '');
            $day = isset($day) ? $day : $d;
            $user_ids = $this->getSubIds($user->user_id);
            $where = [];

            if ($day) {
                $where[] = ['store_day_all_orders.day', '=', $day];
            }
            

            //测试  上线的时候这两行注释掉
            // $store_ids[0]['store_id']='2019091017081561515';
            // $store_ids[1]['store_id']='2018061205492993161';

            //排序只能按一种进行排序
            /*
                传参
                px                          字段
                px_type-asc/px_type-desc    值
            */
            if ($request->input('px') != '') {
                $px = explode('-', $request->input('px'));
                //如果排序接受的是order_money排序
                if ($px[0] == 'order_money') {
                    // $obj = StoreDayAllOrder::join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                    //     ->where($where)
                    //     ->whereIn('store_day_all_orders.store_id', $store_ids)
                    //     ->orderBy('store_day_all_orders.total_amount', $px[1])
                    //     ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');

                    //检测表  store_day_all_orders
                    if (env('DB_D1_HOST')) {
                        if (Schema::hasTable('store_day_all_orders')) {
                            $obj = DB::connection("mysql_d1")->table('store_day_all_orders')->join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                                ->where($where)
                                ->whereIn('store_day_all_orders.user_id', $user_ids)
                                ->orderBy('store_day_all_orders.total_amount', $px[1])
                                ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
                        }
                    } else {
                        if (Schema::hasTable('store_day_all_orders')) {
                            $obj = StoreDayAllOrder::join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                                ->where($where)
                                ->whereIn('store_day_all_orders.user_id', $user_ids)
                                ->orderBy('store_day_all_orders.total_amount', $px[1])
                                ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
                        }
                    }
                }

                //如果排序接受的是order_count排序
                if ($px[0] == 'order_count') {
                    // $obj = StoreDayAllOrder::join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                    //     ->where($where)
                    //     ->whereIn('store_day_all_orders.store_id', $store_ids)
                    //     ->orderBy('store_day_all_orders.order_sum', $px[1])
                    //     ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');

                    //检测表  store_day_all_orders
                    if (env('DB_D1_HOST')) {
                        if (Schema::hasTable('store_day_all_orders')) {
                            $obj = DB::connection("mysql_d1")->table('store_day_all_orders')->join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                                ->where($where)
                                ->whereIn('store_day_all_orders.user_id', $user_ids)
                                ->orderBy('store_day_all_orders.order_sum', $px[1])
                                ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
                        }
                    } else {
                        if (Schema::hasTable('store_day_all_orders')) {
                            $obj = StoreDayAllOrder::join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                                ->where($where)
                                ->whereIn('store_day_all_orders.user_id', $user_ids)
                                ->orderBy('store_day_all_orders.order_sum', $px[1])
                                ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
                        }
                    }
                }
            } else {
                //如果没有接受到排序参数//默认查询
                // $obj = StoreDayAllOrder::join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                // ->where($where)
                // ->whereIn('store_day_all_orders.store_id', $store_ids)
                // ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
                if (env('DB_D1_HOST')) {
                    if (Schema::hasTable('store_day_all_orders')) {
                        $obj = DB::connection("mysql_d1")->table('store_day_all_orders')->join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                            ->where($where)
                            ->whereIn('store_day_all_orders.user_id', $user_ids)
                            ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
                    }
                } else {
                    if (Schema::hasTable('store_day_all_orders')) {
                        $obj = StoreDayAllOrder::join('stores', 'store_day_all_orders.store_id', '=', 'stores.store_id')
                            ->where($where)
                            ->whereIn('store_day_all_orders.user_id', $user_ids)
                            ->select('stores.store_short_name', 'store_day_all_orders.total_amount', 'store_day_all_orders.order_sum', 'store_day_all_orders.store_id');
                    }
                }
            }

            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            $this->status = 1;
            $this->message = '数据返回成功';
	    
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    public function settle_order(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $user_id = $request->get('user_id', '');
            $pay_status = $request->get('status', '');
            $time_start_s = date('Y-m-d 00:00:00', time());
            $time_start_e = date('Y-m-d 23:59:59', time());
            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');

            if (in_array($time_start, [null, ''])) {
                $time_start = $time_start_s;
            }

            if (in_array($time_end, [null, ''])) {
                $time_end = $time_start_e;
            }
	    
            $re_data = [
                'time_start' => $time_start,
                'time_end' => $time_end,
            ];
	    
            $check_data = [
                'time_start' => '订单开始时间',
                'time_end' => '订单结束时间',
            ];
            $check = $this->check_required($re_data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $out_trade_no = $request->get('out_trade_no', '');
            $trade_no = $request->get('trade_no', '');

            $day = date('Ymd', strtotime($time_start));
            $table = 'settle_orders_' . $day;

            if ($out_trade_no || $trade_no) {
                $a = array('a', '_', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
                $b = str_ireplace($a, "", $out_trade_no);
                $day = substr($b, 0, 8);
                $table = 'settle_orders_' . $day;
            }

            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 1;

            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => '时间跨度不能超过' . $day . '天'
                ]);
            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table($table);
            } else {
                $obj = DB::table($table);
            }

            $where = [];
            if ($user_id == "") {
                $user_id = $user->user_id;
            }

            $user_ids = $this->getSubIds($user_id);

            if ($pay_status) {
                $where[] = ['pay_status', '=', $pay_status];
            }
	    
            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];
            }

            if ($time_start) {
                $where[] = ['created_at', '>=', $time_start];
            }
	    
            if ($time_end) {
                $where[] = ['created_at', '<=', $time_end];
            }

            if ($out_trade_no || $trade_no) {
                if ($out_trade_no) {
                    $where1[] = ['out_trade_no', $out_trade_no];
                }

                if ($trade_no) {
                    $where1[] = ['trade_no', $trade_no];
                }

                $obj = $obj->where($where1)
                    ->whereIn('user_id', $user_ids)
                    ->orderBy('created_at', 'desc');

            } else {
                $obj = $obj->where($where)
                    ->whereIn('user_id', $user_ids)
                    ->orderBy('created_at', 'desc');
            }

            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            $this->status = 1;
            $this->message = '数据返回成功';
	    
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = 2;
            $this->message = '暂无数据';
            return $this->format([]);
        }
    }


    /**
     * 刷脸支付（去重）统计
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function face_payment_statistics(Request $request)
    {
        $token = $this->parseToken();
        $token_user_id = $token->user_id;

        $start_time = $request->post('time_start', date('Y-m-d 00:00:00', time())); //开始时间
        $end_time = $request->post('time_end', date('Y-m-d 23:59:59', time())); //结束时间
        $user_id = $request->post('user_id', '');  //服务商id
        $device_id = $request->post('device_id', '');  //设备id
        $amount_start = $request->post('amount_start', 2.00); //交易开始金额
        $amount_end = $request->post('amount_end', ''); //交易结束金额
        $pay_status = $request->post('pay_status', 1); //支付状态

        try {
            $login_user = User::where('id', $token_user_id)
                ->where('is_delete', '=', '0')
                ->first();
            if (!$login_user) {
                $this->status = '2';
                $this->message = '当前登录状态异常';
                return $this->format();
            }

            if (isset($user_id) && ($user_id != 'undefined') && $user_id) {
                $user_ids = $this->getSubIds($user_id);
            } else {
                $user_ids = $this->getSubIds($token_user_id);
            }

            $where = [];
            if ($amount_start) {
                $where[] = ['o.total_amount', '>=', "$amount_start"];
            }
            if ($amount_end) {
                $where[] = ['o.total_amount', '<=', "$amount_end"];
            }
            if ($device_id) {
                $where[] = ['o.device_id', "$device_id"];
            }
            if ($pay_status) {
                $where[] = ['o.pay_status', "$pay_status"];
            }

            $obj = DB::table('orders as o')
                ->select([
                    DB::raw("COUNT(distinct(`o`.`buyer_id`)) as `count_order`"),
                    'o.device_id',
                    'o.store_name',
                    'o.pay_status',
                    'o.pay_status_desc',
                    'o.ways_source',
                    'o.ways_source_desc',
                    DB::raw("date_format(o.pay_time, '%Y-%m-%d') AS `modify_pay_time`")
                ])
                ->where($where)
                ->where('o.pay_time', '>=', "$start_time")
                ->where('o.pay_time', '<=', "$end_time")
                ->whereIn('o.pay_method', ['alipay_face', 'weixin_face'])
                ->whereIn('o.user_id', $user_ids)
                ->groupBy(['modify_pay_time', 'o.device_id'])
                ->orderBy('modify_pay_time', 'asc');

            $this->t = count($obj->get()->toArray());
            $data = $this->page($obj)->get();
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }

        $this->status = '1';
        $this->message = '数据返回成功';
        return $this->format($data);
    }


    /**
     * 微信分账流水
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function profit_sharing_lists(Request $request)
    {
        $token = $this->parseToken();
        $token_user_id = $token->user_id;

        $start_time = $request->post('time_start', ''); //开始时间
        $end_time = $request->post('time_end', ''); //结束时间
        $user_id = $request->post('user_id', '');  //服务商id
        $store_id = $request->post('store_id', '');  //门店id
        $amount_start = $request->post('amount_start', ''); //交易开始金额
        $amount_end = $request->post('amount_end', ''); //交易结束金额
        $fz_status = $request->post('fz_status', ''); //状态

        $login_user = User::where('id', $token_user_id)
            ->where('is_delete', '=', '0')
            ->first();
        if (!$login_user) {
            $this->status = '2';
            $this->message = '当前登录状态异常';
            return $this->format();
        }

        if (isset($user_id) && ($user_id != 'undefined') && $user_id) {
            $user_ids = $this->getSubIds($user_id);
        } else {
            $user_ids = $this->getSubIds($token_user_id);
        }

        $where = [];
        if ($amount_start) {
            $where[] = ['fz_amount', '>=', "$amount_start"];
        }
        if ($amount_end) {
            $where[] = ['fz_amount', '<=', "$amount_end"];
        }
        if ($store_id) {
            $where[] = ['store_id', "$store_id"];
        }
        if ($fz_status) {
            $where[] = ['status', "$fz_status"];
        }
        if (!$start_time) {
            $start_time = date('Y-m-d 00:00:00', time());
        }
        if (!$end_time) {
            $end_time = date('Y-m-d 23:59:59', time());
        }

        try {
            $obj = WeixinSharingOrder::where($where)
                ->where('updated_at', '>=', "$start_time")
                ->where('updated_at', '<=', "$end_time")
                ->whereIn('user_id', $user_ids)
                ->orderBy('updated_at', 'desc');

            $this->t = count($obj->get()->toArray());
            $data = $this->page($obj)->get();
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }

        $this->status = '1';
        $this->message = '数据返回成功';
        return $this->format($data);
    }


    /**
     * 刷脸设备（去重）统计
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function face_payment_dis(Request $request)
    {
        $token = $this->parseToken();
        $token_user_id = $token->user_id;

        $start_time = $request->post('time_start', date('Y-m-d 00:00:00', time())); //开始时间
        $end_time = $request->post('time_end', date('Y-m-d 23:59:59', time())); //结束时间
        $user_id = $request->post('user_id', '');  //服务商id
        $device_id = $request->post('device_id', '');  //设备id
        $amount_start = $request->post('amount_start', 2.00); //交易开始金额
        $amount_end = $request->post('amount_end', ''); //交易结束金额
        $pay_status = $request->post('pay_status', 1); //支付状态

        try {
            $login_user = User::where('id', $token_user_id)
                ->where('is_delete', '=', '0')
                ->first();
            if (!$login_user) {
                $this->status = '2';
                $this->message = '当前登录状态异常';
                return $this->format();
            }

            if (isset($user_id) && ($user_id != 'undefined') && $user_id) {
                $user_ids = $this->getSubIds($user_id);
            } else {
                $user_ids = $this->getSubIds($token_user_id);
            }

            $where = [];
            if ($amount_start) {
                $where[] = ['total_amount', '>=', "$amount_start"];
            }
            if ($amount_end) {
                $where[] = ['total_amount', '<=', "$amount_end"];
            }
            if ($device_id) {
                $where[] = ['device_id', "$device_id"];
            }
            if ($pay_status) {
                $where[] = ['pay_status', "$pay_status"];
            }

            $obj = DB::table('orders')
                ->select([
                    DB::raw("COUNT(distinct(`buyer_id`)) as `count_order`"),
                    'device_id',
                    'store_name',
                    'pay_status',
                    'pay_status_desc',
                    'ways_source',
                    'ways_source_desc'
                ])
                ->where($where)
                ->where('pay_time', '>=', "$start_time")
                ->where('pay_time', '<=', "$end_time")
                ->whereIn('pay_method', ['alipay_face', 'weixin_face'])
                ->whereIn('user_id', $user_ids)
                ->groupBy('device_id');

            $this->t = count($obj->get()->toArray());
            $data = $this->page($obj)->get();
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }

        $this->status = '1';
        $this->message = '数据返回成功';
        return $this->format($data);
    }
    
    //获取代理
    public function agent_list (Request $request)
    {
        $token = $this->parseToken();

        $obj = DB::table('users');
        $type = $request->get('type');
        if ($token->pid != 0) {
            $where[] = ['id', '=', $token->user_id];
        }
        if ($type == "2") {
            $where[] = ['pid', '=', '1'];
        }

        $obj = $obj->where($where)->select('id', 'name', 'level_name');
        $this->t = $obj->count();
        $data = $this->page($obj)->get();
        $this->status = 1;
        $this->message = '数据返回成功';
        return $this->format($data);
    }

    public function getAllSub ($data, $pid)
    {
        static $arr = [];
        foreach($data as $v){
            if($v->pid == $pid){
                $arr[] = $v->id;
                $this->getAllSub($data,$v->id);
            }
        }
        return $arr;
    }
    
    public function arrearage_merchant(Request $request)
    {
        try {
            $token = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $wheres[] = ['status', '=', 1];
            if(!empty($store_id)){
                $wheres[] = [ 'store_id', '=',  $store_id];
            }
            $userIds = [];
            if ($token->pid != 0) {
                if ($token->pid == 1) {
                    $all = DB::table('users')->get()->toArray();
                    $userIds = $this->getAllSub($all, $token->user_id);
                    $userIds[] = (int)$token->user_id;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => "暂无权限"
                    ]);
                }

            }

            $wheres[] = ['goods_cost_balance', '<', 0];
            $obj = DB::table('stores');
            $obj->where($wheres)->select('store_id', 'store_name', 'people_phone', 'goods_cost_balance')
                ->orderBy('id', 'desc');

            if ($userIds) {
                $obj->whereIn('user_id', $userIds);
            }
            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            if( !isset($data) ){
                $this->status = -1;
                $this->message = '暂无数据';
                return $this->format();
            }
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    
    /**
     * 单品
     */
    function goods_count(Request $request){
        try {
            $token = $this->parseToken();
            $export = $request->get('export', '');

            $time_start = $request->get('time_start', '');
            $time_end = $request->get('time_end', '');
            $store_id = $request->get('store_id', '');
            $user_id = $request->get('user_id', '');
            $wheres[] = ['status', '=', 1];
            if(!empty($store_id)){
                $wheres[] = [ 'store_id', '=',  $store_id];
            }
            $userIds = [];
            if(!empty($user_id)){
                $all = DB::table('users')->get()->toArray();
                $userIds = $this->getAllSub($all, $user_id);
                $userIds[] = (int)$user_id;
            } else {
                if ($token->pid != 0) {
                    if ($token->pid == 1) {
                        $all = DB::table('users')->get()->toArray();
                        $userIds = $this->getAllSub($all, $token->user_id);
                        $userIds[] = (int)$token->user_id;
                    } else {
                        return json_encode([
                            'status' => 2,
                            'message' => "暂无权限"
                        ]);
                    }

                }
            }
            if( empty($time_start) || empty($time_end) ){
                $this->status = -1;
                $this->message = '请选择时间';
                return $this->format();
            }
            //限制时间
            $date = (strtotime($time_end) - strtotime($time_start)) / 86400;
            $day = 31;
            if ($date > $day) {
                return json_encode([
                    'status' => 2,
                    'message' => "时间筛选不能超过 $day 天"
                ]);
            }
            if( empty($time_end) ){
                $time_end = date('Y-m-d 23:59:59', time());
            } else {
                if ($time_start != $time_end) {
                    $date = ((strtotime($time_end) > strtotime($time_start)))?true:false;
                    if (!$date) {
                        return json_encode([
                            'status' => 2,
                            'message' => '开始时间不能超过结束时间'
                        ]);
                    }
                }
            }
//            DB::connection()->enableQueryLog();
            $obj = DB::table('order_goods_details as o');
            $obj->where($wheres)
                ->leftjoin('users', 'users.id', '=', 'o.user_id')
                ->orderBy('o.id', 'desc')
                ->select('users.name', 'o.*')
                ->where('o.created_at', '>=', $time_start.' 00:00:00')
                ->where('o.created_at', '<=', $time_end.' 23:59:59');

            if ($userIds) {
                $obj->whereIn('user_id', $userIds);
            }
            if ($export == 1) {
                $dataArray = $obj->select(
                    'users.name',
                    'o.store_id',
                    'o.store_name',
                    'o.out_trade_no',
                    'o.coupon_name',
                    'o.num',
                    'o.coupon_type',
                    'o.cost',
                    'o.agent_cost',
                    'o.company',
                    'o.created_at'
                )->get()->toArray();
                $filename = '单品对账导出.csv';
                $tileArray = ['代理商','门店ID', '门店名称', '订单号', '单品名称', '核销数量', '优惠类型', '服务费', '代理收益', '通道', '时间'];
                $dataArray = json_decode(json_encode($dataArray), true);

                return $this->exportToExcel($filename, $tileArray, $dataArray);
            } else {
                $this->t = $obj->count();
                $data = $this->page($obj)->get();
//            dd(DB::getQueryLog());
                if( !isset($data) ){
                    $this->status = -1;
                    $this->message = '暂无数据';
                    return $this->format();
                }
                return $this->format($data);
            }
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    public function exportToExcel($filename, $tileArray = [], $dataArray = [])
    {
        ini_set('memory_limit', '512M');
        ini_set('max_execution_time', 0);
        ob_end_clean();
        ob_start();
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=" . $filename);
        $fp = fopen('php://output', 'w');
        fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));//转码 防止乱码(比如微信昵称(乱七八糟的))

//        fputcsv($fp, $s_array);
        fputcsv($fp, []);
        fputcsv($fp, $tileArray);
        $index = 0;
        foreach ($dataArray as $item) {
            if(!empty($item['out_trade_no'])){
                $item['out_trade_no'] = $item['out_trade_no']. "\t";
            }
            if(!empty($item['store_id'])){
                $item['store_id'] = $item['store_id']. "\t";
            }
            $index++;
            fputcsv($fp, $item);
        }

        ob_flush();
        flush();
        ob_end_clean();
    }


}
